Clear Global Workspace from GUI Pushbutton
8 views (last 30 days)
Show older comments
Is there a way to clear the global (main) workspace of matlab using a pushbutton of the Guide GUI ?
0 Comments
Answers (2)
per isakson
on 15 Jun 2013
Edited: per isakson
on 15 Jun 2013
"global (main) workspace" I assume/guess you refer to the base workspace.
Try something like
evalin( 'base', 'clearvars *' )
or
evalin( 'base', 'clear variables' )
in the callback function of the pushbutton. See the documentation on clearvars and on clear
6 Comments
per isakson
on 15 Jun 2013
Edited: per isakson
on 15 Jun 2013
No, it is not enough for me to be able to help.
IMO: The description of the concept of workspace in the Matlab documentation is not good. I've tried to find something to link to.
I wrote "I assume/guess you refer to the base workspace". You did not indicate the I'm mistaken. And I still think you mean the base workspace.
Anyhow, there is
- a base workspace, which is used when working interactively, by scripts called from the base workspace, and finally callback functions are invoked from the base workspace
- each function has a separate workspace, which is cleared when the execution of the function ends
global variables reside in the global workspace according to the documentation. This is the first time I noticed "global workspace" in the documentation. (I never use global variables). AFAIK: the global workspace cannot be displayed in the Workspace Browser.
I don't think there is any reference to "main workspace" in the documentation. I find it confusing to call the workspace of the global variables the main workspace.
Thus, is it the global variables you want to clear?
If "nothing works" try read the instruction! Did you read the documentation on clear and clearvars?
Finally, I repeat: "What have you tried that did not work?"
Image Analyst
on 15 Jun 2013
I don't think so since I told him about "clear global" to clear the global workspace and he said he tried that and it didn't do what he wanted. I agree that he needs to read up on workspaces and understand what they are and when they're in scope. Read up on what "in scope" means for that matter. Then use proper terminology and show us the actual code including the larger context of where he's calling it so we can see why it didn't work for him like it did for me.
Image Analyst
on 15 Jun 2013
You can try
clear global;
though I'm not sure that will get everything in the base workspace (doubt it), or just things declared as global (think so). Why do you think you want or need to do this anyway? You can always do clearvars before you run your script or function.
2 Comments
Image Analyst
on 15 Jun 2013
Each function in GUIDE has its own workspace, that is separate from the base workspace. How are you accessing the variables in the base workspace? And I ask again, why do you think it's necessary to clear those? They are not in scope and you won't see them or have access to them unless you used evalin() for some bizarre, unnecessary reason. It's just NOT NECESSARY to clear them.
If you do want to clear them for some reason, you can use the code per gace you:
evalin( 'base', 'clearvars *' )
Put it in the i_OpeningFcn() in your GUIDE generated m-file, around line 57. I tried it, and it works. I had variables in the base workspace, ran my code with that in the OpeningFcn(), and it got rid of them. But again, you don't need to.
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!