how to save loop result in a specified folder, where the folder name predefined according to the iteration parameter

1 Ansicht (letzte 30 Tage)
Suppose I have the following code
U_range=20:25;
for i=1:6
U=U_range(i);
Result=U^2;
save('C:\Users\KKB\Documents\U=20\Result.mat','Result'); % Only for U=20,Modification required here ????
end
I want to save All Result (here six), in the specified path i.e.,C:\Users\KKB\Documents, but the folder name where the six Result will be saved are respectively U=20,U=21,U=22,U=23,U=24 and U=25. It should be noted that six empty folder having the above mentioned name are already created in the specified path. Only the Result need to be save in those respective folder. How to do that?

Akzeptierte Antwort

David Barry
David Barry am 17 Dez. 2016
Bearbeitet: David Barry am 17 Dez. 2016
You can use num2str on your loop iterator to build up the folder name. For example:
for ii = 1:3
a = rand(ii);
fdr = ['/Users/davidbarry/Documents/MATLAB/', 'U=', num2str(ii)];
if ~exist(fdr, 'dir')
mkdir(fdr);
end
save([fdr, '/Result.mat'], 'a');
end
By the way, I would avoid having equals symbol in the folder or file name but that's up to you.

Weitere Antworten (0)

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by