Access data in different files stored in different

2019-08-04 12:00发布

问题:

I've searched around and found some potential solutions for my problem but have been unable to implement the code.

Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).

I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.

Also, the names of the sub-folders and the files within are known:

Folders = TT1, TT2, etc.

Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.

I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:

TTs  = [1,2,3,4,5]; % List of sub-folders to use.

for i = TTs; 

    addpath(strcat('TT',num2str(i))); 
    cd (strcat('TT',num2str(i)));     

        for w = 1:4;  %get data from the 4 files
            load(strcat('TT',num2str(i),'ch', num2str(w), '.mat')); 
            allChs(w,:) = data(1,:);  %concatenate into one matrix
        end

    cd ..
    rmpath(strcat('TT',num2str(i)));
end

With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...

I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.

Cheers, Oiko

回答1:

As @Cris Luengo said, you don't need add to path a folder for reading from it.

Also, you don't need cd, you better explicit the path you want to read from:

parentPath = <your-main-folder>;
TTs  = [1,2,3,4,5]; % List of sub-folders to use.

Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:

for k = TTs; 
   for w = 1:4;  %get data from the 4 files
       load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat'))); 
       allChs(4*(k-1)+w,:) = data(1,:);  %concatenate into one matrix
   end
end