2010-11-11 54 views
2

我有大量需要從實驗室工作中處理的數據。我有很多包含尺寸爲7 x w的信號矩陣的.mat文件。我需要將矩陣大小調整爲7 x N,並且w大於和小於N,以便使分析的其餘部分更加容易(不必關心N之後的數據)。我有我想如何工作,但不知道如何實現它的psuedocode。任何幫助將非常感謝!我的所有數據的自動加載多個* .mat文件和矩陣調整大小

文件夾結構:

主文件夾

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

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... 

回答

2

首先,你需要生成文件列表。我有我自己的功能,但有,例如,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×n-nFiles數組,其中Master_matrix的第n個平面是第n個文件的內容。在這種情況下,你會初始化Master_matrix作爲

Master_matrix = zeros(7,N,nFiles); 

,你會寫的,如果子句爲

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 

另外請注意,您可能要初始化Master_matrixNaN,而不是zeros,所以零不會影響後續的統計信息(如果這是您想要對數據進行處理的話)。