How to clear the workspace from within a program?

253 vues (au cours des 30 derniers jours)
Kerry Hipps
Kerry Hipps le 14 Mar 2019
Réponse apportée : Brian le 24 Sep 2024
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 commentaires
KW Hipps
KW Hipps le 19 Mar 2019
Modifié(e) : per isakson le 31 Mar 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
Stephen23 le 20 Mar 2019
Modifié(e) : Stephen23 le 31 Mar 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?

Connectez-vous pour commenter.

Réponses (4)

Yasasvi Harish Kumar
Yasasvi Harish Kumar le 14 Mar 2019
Modifié(e) : Yasasvi Harish Kumar le 14 Mar 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

per isakson
per isakson le 14 Mar 2019
Modifié(e) : per isakson le 14 Mar 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.

Edwin Henry Jara Bardales
Edwin Henry Jara Bardales le 16 Mar 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.

Brian
Brian le 24 Sep 2024
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

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by