Effacer les filtres
Effacer les filtres

Save command: List of variables to save can vary

2 vues (au cours des 30 derniers jours)
CAM
CAM le 27 Jan 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])

Réponse acceptée

Andrew Newell
Andrew Newell le 27 Jan 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{:})

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by