How to clear the workspace from within a program?
248 次查看(过去 30 天)
显示 更早的评论
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
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
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?
回答(4 个)
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
0 个评论
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.
0 个评论
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.
0 个评论
Brian
2024-9-24
I frequently find myself typing both a clear and clc command to clean up my Command Window and Workspace. I created a function to do both. It will remove all variables from the workspace regardless of how or when they were created:
% cell array of workspace variables
workspaceVariables = evalin('base', 'who');
% number of elements in cell array
nElements = numel(workspaceVariables);
% unicode value of a space
unicodeSpace = 32;
% clear variables from workspace
for element = 1:nElements
evalin('base', strcat('clear', unicodeSpace, char(workspaceVariables{element})));
end
% clear command window
clc
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!