Sampi
Sampi

Reputation: 105

Loading multiple .mat files in the workspace

I want to load multiple .mat files (around 500 in number) into my workspace. The files are named as

omni_AP1_trial_1_loc_1.mat 
omni_AP1_trial_1_loc_2.mat 
omni_AP1_trial_1_loc_3.mat
.
.
omni_AP1_trial_1_loc_57.mat
.
. 
omni_AP1_trial_10_loc_1.mat 
omni_AP1_trial_10_loc_2.mat 
omni_AP1_trial_10_loc_3.mat 
.
.
omni_AP1_trial_10_loc_57.mat

I am using the given code :

files_1 = dir('omni_AP1_trial_*_loc_1.mat');
NumberOfDataset = length(files_1);

for i = 1:NumberOfDataset  
    %get allfiles matching the pattern 'dataset(i)_*'
    files = dir(sprintf('omni_AP1_trial_%d_loc_*.mat',i));
    for j = 1:length(files)
       fprintf('Current file : %s\n',files(j).name)
       a= load(files(j).name);

    end
end

During execution even though the fprintf statement shows consecutive files being picked, but the structure a holds only the last file that was picked up and the previous file is being overwritten when the loop iterates.

How can I have all the files loaded together in the workspace ? Please help.

Upvotes: 0

Views: 1208

Answers (3)

Sampi
Sampi

Reputation: 105

In order to make it simpler I tried using

for jj=1:57
    for ii = 1: 10
 a(ii,jj) =load(['omni_AP1_trial_' num2str(ii) '_loc_' num2str(jj)'.mat']);
    end
end

I have a total of 57 locations which are mentioned as loc_1 to loc_57 and for each location I am taking 10 trials and these are mentioned like trial_1_loc_1, trial_2_loc_1 upto trial_10_loc_1.

In the above code when I don't use the first loop (ie, jj) and when I am replacing num2str(jj) by 1 (ie location_1), then in that case I am getting 10 trials for location 1. When I change that to 2, then I am getting 10 trials for location 2 and so on.

So when I used one loop only, it worked and the code is:

for ii = 1: 10
 a(ii) =load(['omni_AP1_trial_' num2str(ii) '_loc_1.mat']);
end

The above code worked fine. So now I am thinking to use 2 loops one for the location (jj loop) and another for the number of trials (ii loop). I want to use the loop in such a way that for my first location,it will load 10 trials and for the next it will load another 10 trials and so on. When I am using both the loops, I am getting an error (Unexpected matlab operator). Don't know what is wrong. I am not sure the logic I am using is correct or not. If the logic is correct then what is the problem on the above code when I am using 2 loops to load all the files?

Upvotes: 0

dasdingonesin
dasdingonesin

Reputation: 1358

You can create an array of structs for your results in the iteration:

a = []; % create empty array
files_1 = dir('omni_AP1_trial_*_loc_1.mat');
NumberOfDataset = length(files_1);

for i = 1:NumberOfDataset  
    %get allfiles matching the pattern 'dataset(i)_*'
    files = dir(sprintf('omni_AP1_trial_%d_loc_*.mat',i));
    for j = 1:length(files)
       fprintf('Current file : %s\n',files(j).name)

       a(end+1)= load(files(j).name); % store data in struct array

    end
end

Upvotes: 3

xenoclast
xenoclast

Reputation: 1635

Your a value is being overwritten each time, but you can have an array of structures:

a(j) = load(files(j).name);

The next question is usually how to index the same subelement in multiple struct array elements. If subelement is a scalar field of the variable that's stored in the files (and if that variable has the same name in each file) you can do

[a(:).variablename.subelement]

Upvotes: 2

Related Questions