Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

David Barry
David Barry el 17 de Dic. de 2016
Editada: David Barry el 17 de Dic. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by