MATLAB Answers

Question about load .mat files

3 views (last 30 days)
Tommy
Tommy on 7 Jul 2012
I have a group of .mat files, which have continuous number of name, i.e. from W_star_1.mat to W_star_300.mat. Could I write a code using "for i=1:300" loop to load all of them instead of loading them one by one? Thank you very much.

  0 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 7 Jul 2012
Edited: the cyclist on 7 Jul 2012
If you are certain that there is no overlap in variables in all those files, then you can use
for i = 1:300
load(['W_star_',num2str(i),'.mat'])
end
but if some of the variables overlap, you'll need to load them into separate structures, to ensure that later loads don't overwrite earlier ones.
for i = 1:300
S{i} = load(['W_star_',num2str(i),'.mat'])
end
Then you'll need to extract the variables from the structures.

  0 Comments

Sign in to comment.

More Answers (1)

mohammad
mohammad on 7 Jul 2012
First you can save them when all of them are on the workspace
save(['name', '.mat']);
then use
load(['name', '.mat']);

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags