필터 지우기
필터 지우기

How can I clear all the variables except one before restarting my program?

조회 수: 2 (최근 30일)
Is there a way to clear all of the variables except one when I end a game I wrote so that the next time it starts the variables have to be re-entered?
I have a program where it runs while program == 1, so when I ask the user if they want to play again, if they don't answer yes, then program = 0 and the program ends, so if I could clear all the variables except program, that would be super convenient.
  댓글 수: 3
Image Analyst
Image Analyst 2015년 11월 30일
Or save what you want to a .mat file. Then you can recall variables from the .mat file at each run. This method will even recall variables across different sessions of MATLAB, unlike methods using clear().

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2015년 11월 29일
Here's a snippet to study:
a = 10 % Declare sample variables.
b = 33;
result = 20;
% Get the names of the variables.
vars = whos
varNames = {vars.name};
celldisp(varNames);
% Let's loop over names, keeping the variable called "result"
for k = length(varNames): -1 : 1
fprintf('Checking %s\n', varNames{k});
if strcmp(varNames{k}, 'result')
continue;
end
fprintf(' Deleting %s\n', varNames{k});
clear(varNames{k});
end
clear ('k', 'varNames', 'vars'); % Get rid of loop iterator and other temporary items.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by