Greetings,
How can I write the following command in a short form to avoid this repetition?
clear
%%load all Residual .mat files
s = what; %look in current directory
%s=what('dir') %change dir for your directory name
matfiles=s.mat
for a=1:numel(matfiles)
load(char(matfiles(a)))
end
SDED = vertcat(SDED1,SDED2,SDED3,SDED4,SDED5,SDED6, ...
SDED7,SDED8,SDED9,SDED10,SDED11,SDED12);
save SDED.mat SDED
Thank you for the help

 Réponse acceptée

Jan
Jan le 28 Oct 2015
Modifié(e) : Jan le 30 Oct 2015

1 vote

s = dir('*.mat');
C = cell(1, numel(s));
for a = 1:numel(s)
Data = load(s(a).name); % [EDITED]
% Now assuming that "SDED1" e.g. is the only variable in the MAT file:
C(a) = struct2cell(Data);
end
SDED = vertcat(C{:});
save SDED.mat SDED

3 commentaires

Lilya
Lilya le 29 Oct 2015
thank you for your help Jan I am getting this error
Cell contents reference from a non-cell array object
I changed the curly brackets, but it still warns me about the error
Jan
Jan le 30 Oct 2015
See [EDITED]
Lilya
Lilya le 30 Oct 2015
Appreciated Jan

Connectez-vous pour commenter.

Plus de réponses (1)

Thorsten
Thorsten le 28 Oct 2015
Modifié(e) : Thorsten le 28 Oct 2015

0 votes

If you know that the SDED are numbered from 1 to 12:
s = sprintf('SDED%d, ', 1:12);
eval(['SDED = vertcat(' s(1:end-2) ');'])
or if you want to use all SDED variables in the workspace:
x = whos('SDED*');
s = sprintf('%s, ', x.name);
eval(['SDED = vertcat(' s(1:end-2) ');'])

2 commentaires

Better to avoid eval and simply load the data into one structure:
S = load(...);
Why eval is poor code that should be avoided:
Lilya
Lilya le 29 Oct 2015
thank you all, it works

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Question posée :

le 28 Oct 2015

Commenté :

le 30 Oct 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by