Save command: List of variables to save can vary
1 次查看(过去 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])
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!