How do I save or load my variables to or from files with similar names?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
For example, within a FOR loop, I calculate values for my variables 'a', 'b', and 'c', ten times. I want to save these variables for each iteration to MAT-files with names test01.mat, test02.mat....test10.mat.
Akzeptierte Antwort
MathWorks Support Team
am 27 Jun. 2009
You can use string concatenation to create the strings for your filenames:
a = 1;
b = 2;
c = 3;
filename = 'test';
for i = 1:10
a = a + 1;
b = b + 2;
c = c + 3;
str_counter = num2str(i);
if i < 10
str_counter = ['0' str_counter]; %create '01' - '09' strings for 'test01.mat' - 'test09.mat'
end
new_filename = [filename str_counter]; %concatenate 'test' to the test number
save(new_filename,'a','b','c')
end
For more information on string concatenation, type
help horzcat
and
help strcat
at the MATLAB command prompt.
For information on how to import multiple files into the MATLAB workspace, see the Related Solution.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!