Meaning of "clear all" and "clf"

156 ビュー (過去 30 日間)
Andres Bayona
Andres Bayona 2019 年 8 月 14 日
コメント済み: Dr Sreelatha P 2020 年 10 月 27 日
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.
  1 件のコメント
Dr Sreelatha P
Dr Sreelatha P 2020 年 10 月 27 日
Clear Current figure window

サインインしてコメントする。

採用された回答

Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
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 件のコメント
Rik
Rik 2019 年 8 月 14 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by