필터 지우기
필터 지우기

Save command: List of variables to save can vary

조회 수: 2 (최근 30일)
CAM
CAM 2012년 1월 27일
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