How can I copy variables of a GUI to the workspace?

3 views (last 30 days)
I'm developing a GUI (using both programmatic and GUIDE approaches) and, for debug purpose it would be REALLY useful to see its variables in my workspace.
I save my variables as handles.myvar = value.
How can I display all variables created by the GUI in my workspace?
Thank you

Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 14 Aug 2012
you declare them Global in both workspace and your gui functions
global var1 var2 ...
  2 Comments
Matt Kindig
Matt Kindig on 14 Aug 2012
In general, you should NOT use global variables! Global variables have a number of problems, the least of which is that they must be declared using the "global" keyword in BOTH the base workspace and any functions in which they are used. This is very easy to forget! As Matlab does not require variable declarations, if you neglect to include this global declaration, a local variable with the same name will be created in the function scope, causing big problems. This is definitely not a good practice.
Using assigin() is better. setappdata() and getappdata() is also useful.
Matt Fig
Matt Fig on 14 Aug 2012
@Matt, Amen! I have spent way too much time debugging global variables in GUIs. Usually it is when the author has so mucked it up that he is ready to just start over from scratch.

Sign in to comment.


Muruganandham Subramanian
Muruganandham Subramanian on 14 Aug 2012

Image Analyst
Image Analyst on 14 Aug 2012
I'm not sure if you want to send the variable from your local workspace to the base workspace, or if you just want to display the values of the handles structure members. You do know that every function (callback function or one you wrote) has its own workspace, don't you? To display the values of the handle structure fields, just put handles on it's own line, with no semicolon, and it will print out the values to the command window.
handles
If you want to send all local variables from all your functions and callbacks to the base workspace, this is not recommended. It defeats the whole purpose of scope.
If you attach a new variable to handles, you need to call guidata() to get handles to update so that all your other callbacks can see the vairable you attached. Otherwise you're just working on a local copy of handles and the change you made is lost when that function exits. Alternatively, if it's a function you wrote that take in handles as an argument, you can pass handles back out and eventually guidata will get called somewhere.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!