Saving variables within a parfor loop
Mostrar comentarios más antiguos
I wish to save individual variables in my script that are defined in a parfor loop. I defined a function called saveVariables.m that would save a specified variable to a .mat file as follows.
function l = saveVariables(local_filename, variable, index)
save(['simulation_' num2str(index) '\' local_filename], variable)
l = 1;
end
The issue that I have is that MATLAB produces the error
Error using saveVariables (line 4)
Argument must contain a string.
I use the backlash '\' since I am running Matlab on Windows. local_filename is meant to be the string of the name of the file that I wish to save. How can I work around this string error?
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 6 de Sept. de 2015
But what are you passing in for "variable" ? It must be the name of a variable.
You should consider using fullfile() . And you should consider using sprintf() to construct the name.
save( fullfile(sprintf('simulation_%d',index), local_filename), variable)
4 comentarios
Sean Pierre
el 6 de Sept. de 2015
Editada: Sean Pierre
el 6 de Sept. de 2015
Walter Roberson
el 6 de Sept. de 2015
function l = saveVariables(local_filename, variable, index)
save(['simulation_' num2str(index) '\' local_filename], inputname(2))
l = 1;
end
and call with
saveVariables('RD_1pk.mat',RD, j);
Sean Pierre
el 6 de Sept. de 2015
Walter Roberson
el 6 de Sept. de 2015
function l = saveVariables(local_filename, variable, index)
varname = inputname(2);
savestruct.(varname) = variable;
save( fullfile(sprintf('simulation_%d',index), local_filename), '-struct', 'savestruct')
l = 1;
end
Categorías
Más información sobre Scope Variables and Generate Names en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!