Does "clear" command clears the global variables

21 views (last 30 days)
Does clear command clears all the variables in the workspace including global variables. In my code, there are lots of global variables. On running the code multiple times it seems that previous global variables are not getting cleared.
I'm using below command at the start of the Code
clear;

Accepted Answer

Adam
Adam on 27 Jun 2017
Edited: Adam on 27 Jun 2017
doc clear
gives a very clear description of the various arguments to clear and what gets cleared by each instruction.
Simply typing
clear
'removes all variables from the current workspace, releasing them from system memory.' to quote that documentation verbatim.
So as ever with global variables it is a murky mess depending whether they are in your workspace or not. If not then they won't be cleared.
Moral of the story is to simply not use globals and to read the documentation!!
  5 Comments
John D'Errico
John D'Errico on 27 Jun 2017
Wish I could also +1 on the comments made by Adam and Guillaume.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by