自动加载多个 *.mat 文件和矩阵调整大小

发布于 2024-10-02 16:36:13 字数 650 浏览 6 评论 0原文

我有大量的实验室工作数据需要处理。我有大量 .mat 文件,其中包含尺寸为 7 x w 的信号矩阵。我需要将矩阵大小调整为 7 x N,并且 w 比 N 更大和更小,以使其余分析更容易(不关心超过 N 的数据)。我有我希望它如何工作的伪代码,但不知道如何实现它。任何帮助将非常感谢!

我所有数据的文件夹结构:

主文件夹

Alpha 1
    1111.mat
    1321.mat
Alpha 2
    1010.mat
    1234.mat
    1109.mat
    933.mat
Alpha 3
    1223.mat

等。

伪代码:

    Master_matrix = []
    For all n *.mat
        Load n'th *.mat from alpha 1
        If w > N
            Resize matrix down to N
        Else
            Zero pad to N
        End if
    Master_matrix = master_matrix .+ new resized matrix
    End for

rest of my code...

I have a ton of data that needs to be processed from lab work. I have a ton of .mat files that contain a signal matrix of dimensions 7 x w. I need to resize the matrix to 7 x N and w is larger and smaller than N to make the rest of the analysis easier (don't care about data past N). I have the psuedocode of how I want this to work but don’t know how to implement it. Any help would be great thanks!

Folder structure of all my data:

Main folder

Alpha 1
    1111.mat
    1321.mat
Alpha 2
    1010.mat
    1234.mat
    1109.mat
    933.mat
Alpha 3
    1223.mat

etc.

Psudeocode:

    Master_matrix = []
    For all n *.mat
        Load n'th *.mat from alpha 1
        If w > N
            Resize matrix down to N
        Else
            Zero pad to N
        End if
    Master_matrix = master_matrix .+ new resized matrix
    End for

rest of my code...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-10-09 16:36:13

首先您需要生成文件列表。我有自己的功能,但有,例如 GETFILELIST 或出色的交互式 UIPICKFILES 生成文件列表。

获得文件列表后(我假设它是包含文件名的元胞数组),您可以执行以下操作:

nFiles = length(fileList);
Master_matrix = zeros(7,N);

for iFile = 1:nFiles
    %# if all files contain a variable of the same name, 
    %# you can simplify the loading by not assigning an output
    %# in the load command, and call the file by
    %# its variable name (i.e. replace 'loadedData')
    tmp = load(fileList{iFile});
    fn = fieldnames(tmp);
    loadedData = tmp.(fn{1});

    %# find size 
    w = size(loadedData,2);

    if w>=N
       Master_matrix = Master_matrix + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w) = Master_matrix(:,1:w) = loadedData;
    end
end

注意:如果您实际上不想添加数据,而只是将其存储在主数组中,您可以将 Master_matrix 制作成 7×N×nFiles 数组,其中 Master_matrix 的第 n 个平面是第 n 个文件的内容。在这种情况下,您可以将 Master_matrix

Master_matrix = zeros(7,N,nFiles);

if 子句编写为

    if w>=N
       Master_matrix(:,:,iFile) = Master_matrix(:,:,iFile) + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w,iFile) = Master_matrix(:,1:w,iFile) = loadedData;
    end

初始化为 NaN,并将 code> 而不是 zeros,这样零就不会影响后续统计信息(如果您想对数据执行此操作)。

First you need to generate the file list. I have my own function for that, but there is, for example, GETFILELIST or the excellent interactive UIPICKFILES to generate the list of files.

Once you have the file list (I'll assume it's a cell array containing the filenames), you can do the following:

nFiles = length(fileList);
Master_matrix = zeros(7,N);

for iFile = 1:nFiles
    %# if all files contain a variable of the same name, 
    %# you can simplify the loading by not assigning an output
    %# in the load command, and call the file by
    %# its variable name (i.e. replace 'loadedData')
    tmp = load(fileList{iFile});
    fn = fieldnames(tmp);
    loadedData = tmp.(fn{1});

    %# find size 
    w = size(loadedData,2);

    if w>=N
       Master_matrix = Master_matrix + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w) = Master_matrix(:,1:w) = loadedData;
    end
end

Note: In case you don't actually want to add up the data, but simply store it in the master array, you can make Master_matrix into a 7-by-N-by-nFiles array, where the nth plane of Master_matrix is the content of the nth file. In this case, you'd initialize Master_matrix as

Master_matrix = zeros(7,N,nFiles);

and you'd write the if-clause as

    if w>=N
       Master_matrix(:,:,iFile) = Master_matrix(:,:,iFile) + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w,iFile) = Master_matrix(:,1:w,iFile) = loadedData;
    end

Also note that you might want to initialize Master_matrix as NaN instead of zeros, so that the zeros don't affect subsequent statistics (if that's what you want to do with the data).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文