Question

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...
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top