MATLAB: Using variable names for saving data

save

I am running a for loop for parametric sweep. After each loop, the data is to be saved within a .mat file using the 'save' command. How do I provide the function names so that each parameter gets its own .mat file after each loop completion?

Best Answer

for K = 1 : 20
filename = sprintf('data%04d.mat', K);
save(filename, 'VariableName');
end
If you have more than 9999 iterations then increase the 4 to the maximum number of digits you need.
Related Question