How do I save or load my variables to or from files with similar names?

2 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
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 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by