MATLAB Answers

0

Meaning of "clear all" and "clf"

Asked by Andres Bayona on 14 Aug 2019
Latest activity Commented on by Rik
on 14 Aug 2019
Hi, I would like to ask what is the meaning of "clear all" and "clf"?
Example:
% Clearing all variables and arrays
clear all;
% Clearing figures
clf;
I have been trying to figure it out but I haven't been able to do it. I have run a lot of codes with and without these commands but I don't understand the difference between them.
Thank you so much in advance.

  0 Comments

Sign in to comment.

1 Answer

Answer by Neuropragmatist on 14 Aug 2019
 Accepted Answer

Have you checked the help or reference material?
This should always be step 1 if you ever have a Matlab problem/question and generally people are not happy if you ask a question without doing that first.
Very basically (as I use them both) clear all will get rid of all the variables in your current workspace. If you make a bunch of variables at the Matlab command console then use clear all you will see the effect:
% copy and paste this code into your command console:
x = 10;
y = rand(10,10);
z = 'hello';
% somwhere you should have a window called 'workspace' and you will see the variables listed there
% alternatively, run the command 'whos' and you will see them listed there
% now run this:
clear all
% the variables should clear from your workspace, 'whos' will not return a list of any variables
% now run this:
x = 10;
y = rand(10,10);
z = 'hello';
% and then this:
clear x
% do you see the difference?
This command is useful if you want to clear up some space in your memory (probably unlikely nowadays unless you are working with huge arrays) or if you want to start a new project in Matlab and don't want old variables messing things up.
clf is more specific, it will clear an open figure window ready for new plots etc:
% copy and paste this code into your command console:
figure
plot(rand(10,1),rand(10,1),'ko')
% a figure should open up with a nice plot in it
% now run this:
clf
% the figure should be emptied
% now run this:
figure
plot(rand(10,1),rand(10,1),'ko')
% and then this:
cla
% do you see the difference?
Again, if you are testing something at the command console you might want to clear a figure ready for some new test you want to do, although I would usually just open a new figure. In some code clf can be useful because people suggest that when you are using loops clf is faster than opening a new figure, which can be useful if you are running many loops or making many figures. There is a similar function called cla, I'll let you investigate what that does.
Hope this helps,
M.

  1 Comment

Rik
on 14 Aug 2019
A few things:
  • clear all shouldn't be used, because it clears everything and forced some things to be loaded from the slow disk (even SSD is slow compared to RAM). You should use functions to keep your workspace clean, and use clear or clearvars during debugging with scripts.
  • Same goes for clf and cla: use delete with the object handles to remove objects and leave clf and cla for debugging.
  • Berating people is counterproductive. It is good to keep reminding people about the documentation, but an aggressive tone like here or this thread helps nobody. If you feel frustrated, vent it somewhere else.

Sign in to comment.