Does "clear" command clears the global variables

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

If I do not use global variables then I'll have to pass too many arguments in the function call. To make logic clear and simple, I have used some global variables.
What about the command "clear global"
Using global variables never makes logic 'clear and simple'. This question is evidence of that. If it was clear and simple you wouldn't need to ask this! If you have to pass 'too many' variables to the function then equally you have to magic 'too many' variables into your workspace from some unknown place using globals too.
The documentation includes a complete table of what gets cleared when. Surely it is easier for you to just read that than expect someone else to read it for you and then copy it word for word here?!
Click on the 'ItemType' hyperlink in the last line of the Description, for the syntax
clear ItemType
and you get taken to a clear and easy to read table.
If I do not use global variables then I'll have to pass too many arguments in the function call
No. No and No!. That is a bullshit argument, sorry. There are many ways of designing functions to avoid this problem. Starting by refactoring the function so that it does not need so many arguments and if impossible, passing structures, or using classes.
To make logic clear and simple, I have used some global variables.
You may think that it makes logic clear and simple. I'd say it makes debugging impossible since you never know who sets the value of the global variables. It certainly does not make logic clear and simple since it makes it impossible to follow the flow of your program. (where was that value set? Oh! I've got to look into every single function to check it hasn't changed the value of that global).
Global variables are universally despised in any programming language for a reason. They're poor programming practices that went out of fashion last century. Some modern languages do not even allow them.
What about the command "clear global"
Have you read the doc as Adam told you? It's all answered there.
Wish I could also +1 on the comments made by Adam and Guillaume.

Sign in to comment.

More Answers (0)

Asked:

on 27 Jun 2017

Commented:

on 27 Jun 2017

Community Treasure Hunt

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

Start Hunting!