Borrar filtros
Borrar filtros

Save command: List of variables to save can vary

2 visualizaciones (últimos 30 días)
CAM
CAM el 27 de En. de 2012
Is it possible to have the SAVE command save only the variables listed in a string (or cell array)? I need to save different variables in files based on many conditions that come up in the code. Right now I use the "evil" eval-statement to construct a save command. This is an oversimplified example:
if condition1
savelist = 'a b c f '
else
savelist = 'd e f g '
end
% More code and calculations ...
savelist = [savelist, 'w x y z '];
eval(['save ', filename, ' ', savelist])

Respuesta aceptada

Andrew Newell
Andrew Newell el 27 de En. de 2012
Here is how to do it with cell arrays:
a = rand; d = rand; w=rand;
condition1=logical(randi([0 1],1)); % This is just for generating examples
if condition1
savelist = {'a'};
else
savelist = {'d'};
end
savelist = [savelist, {'w'}];
disp(savelist)
filename='testfile.mat';
save(filename,savelist{:})

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices 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