MATLAB Answers

Clear Global Workspace from GUI Pushbutton

Asked by Shadi Al Mahallawy on 15 Jun 2013

Is there a way to clear the global (main) workspace of matlab using a pushbutton of the Guide GUI ?



No products are associated with this question.

2 Answers

Answer by per isakson
on 15 Jun 2013
Edited by per isakson
on 15 Jun 2013

"global (main) workspace" I assume/guess you refer to the base workspace.

Try something like

    evalin( 'base', 'clearvars *' )


    evalin( 'base', 'clear variables' )

in the callback function of the pushbutton. See the documentation on clearvars and on clear


Image Analyst
on 15 Jun 2013

I tried the first line of code and it works just fine - put it in your OpeningFcn() function around line 50 or 60 or so of the m-file.

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.

Answer by 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.


Shadi Al Mahallawy on 15 Jun 2013

Tried it.. Doesn't work. At a certain point in my GUI I need the workspace to be cleared so that when I run my GUI again based on different values, i don't need the old values to be present.

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.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today