How to clear the workspace from within a program?
124 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
KW Hipps
am 19 Mär. 2019
Bearbeitet: per isakson
am 31 Mär. 2019
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
am 20 Mär. 2019
Bearbeitet: Stephen23
am 31 Mär. 2019
" 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?
Antworten (3)
Yasasvi Harish Kumar
am 14 Mär. 2019
Bearbeitet: Yasasvi Harish Kumar
am 14 Mär. 2019
Hi,
You can have clear all as part of your function or script.
Example:
clear all;
a = 10;
b = 5;
sum = a + b;
Regards
0 Kommentare
per isakson
am 14 Mär. 2019
Bearbeitet: per isakson
am 14 Mär. 2019
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.
0 Kommentare
Edwin Henry Jara Bardales
am 16 Mär. 2021
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!