Save command: List of variables to save can vary

2 次查看(过去 30 天)
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])

采纳的回答

Andrew Newell
Andrew Newell 2012-1-27
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{:})

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by