How to clear the workspace from within a program?

조회 수: 296 (최근 30일)
Kerry Hipps
Kerry Hipps 2019년 3월 14일
답변: Edwin Henry Jara Bardales 2021년 3월 16일
I have a program that must have the workspace clear when it runs or it returns invalid results. I don't want the user to need to remember to type CLEAR ALL before running. How can I get the same effect from within a program (at the start)?
  댓글 수: 2
KW Hipps
KW Hipps 2019년 3월 19일
편집: per isakson 2019년 3월 31일
Thank you both! This is how I expected it to work. What is confusing me is demonstrated by this short code segment:
clc;
clear all;
t=input('enter anything');
M=zeros(10,10);
t=input('enter anything');
for i=1:10;
for j=1:10
M(i,j)=i*j
end
end
After running twice, and before responding to the first input, I would expect the M, i, and j variables to be cleared from the workspace. But, they are still there. On the other hand, when I try viewing them by double clicking on them, nothing happens. Is this just a bug in the work space display and the variables are really gone?
Stephen23
Stephen23 2019년 3월 20일
편집: Stephen23 2019년 3월 31일
" Is this just a bug in the work space display and the variables are really gone?"
The variables should be really gone (although keep in mind that the MATLAB GUI might only update once the code has finished running). Both i and j are compiled functions that return the square root of -1, so after you clear your variables from any workspace I would expect the i and j functions to become available again. What you described so far is consistent with that:
>> i = 3;
>> i
i =
3
>> clear all
>> i
ans =
0 + 1i
which leaves only the existence of the M variable unexplained (although you are very vague on which variables exist and how you checked their existence).
Question: how exactly are you checking the existence of these variables?

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

답변 (3개)

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019년 3월 14일
편집: Yasasvi Harish Kumar 2019년 3월 14일
Hi,
You can have clear all as part of your function or script.
Example:
clear all;
a = 10;
b = 5;
sum = a + b;
Regards

per isakson
per isakson 2019년 3월 14일
편집: per isakson 2019년 3월 14일
Your program is that a script or a function? Your question makes me believe your program is a script.
The solution often used in small exercises is to add the line
clear all
as first line of the script. However, this will introduce an undesired side-effect; your program will delete all data the user has in the base workspace.

Edwin Henry Jara Bardales
Edwin Henry Jara Bardales 2021년 3월 16일
Here is the answer: when you are running the code of your script and you have clear or clear all in your code, it will erase your variable values for the next time, but you won't see that on your workspace until the code is finnished, because your script is running yet, but you can prove there are no the variables anymore when you click the variables on your workspace, actually nothing is going to happen, it means that the last values of the previous running was erased.
Most practical prove:
Copy, paste and run this little code on a new script:
clear
clc
a=25;
b=69;
pause(1) %it means that you have to wait 1 second.
Now put the percent symbol before b=69;
Something like this:
clear
clc
a=25;
%b=69;
pause(1) %it means that you have to wait 1 second.
Now run again and you will see that b value isn't anymore on your workspace when pause is over.
Regards.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by