How to clear the workspace from within a program?

249 visualizaciones (últimos 30 días)
Kerry Hipps
Kerry Hipps el 14 de Mzo. de 2019
Respondida: Brian el 24 de Sept. de 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 comentarios
KW Hipps
KW Hipps el 19 de Mzo. de 2019
Editada: per isakson el 31 de Mzo. de 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 el 20 de Mzo. de 2019
Editada: Stephen23 el 31 de Mzo. de 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?

Iniciar sesión para comentar.

Respuestas (4)

Yasasvi Harish Kumar
Yasasvi Harish Kumar el 14 de Mzo. de 2019
Editada: Yasasvi Harish Kumar el 14 de Mzo. de 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 el 14 de Mzo. de 2019
Editada: per isakson el 14 de Mzo. de 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 el 16 de Mzo. de 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 el 24 de Sept. de 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

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by