0

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.

Sampi
  • 87
  • 2
  • 4
  • 14

3 Answers3

3

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
dasdingonesin
  • 1,347
  • 1
  • 10
  • 16
2

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]
xenoclast
  • 1,635
  • 15
  • 26
0

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?

Sampi
  • 87
  • 2
  • 4
  • 14
  • You can do this but it gets complicated and you have to follow more constraints, e.g. every member of the struct array has to have the same fields. You may want to try nesting them instead `location(ii).trial(jj) = load...` – xenoclast Apr 29 '15 at 13:20
  • Also it's better to update the question with followups, if you post an answer with followup questions people might miss it. – xenoclast Apr 29 '15 at 13:21