Most correct way to use global variables across callbacks in GUI

19 Ansichten (letzte 30 Tage)
John
John am 28 Jul. 2014
Beantwortet: Ashish Gudla am 8 Aug. 2014
I read the 2014a GUI maker user guide and it suggests 3 ways to make data available across callbacks. I am writing a moderately sized GUI with GUIDE and the code editor and want to have some "global" data that is visible across all callbacks in the main application figure. By default GUIDE calls this figure "figure1"
Am I correct in saying that the best way to share global data across all call backs is to use:
setappdata(handles.figure1, '<globalVariableName>', <value>)
and in each call back, retrieve the global data using:
getappdata(handles.figure1, <globalVariableName>)
The inconvenience of this is if I want to change the global variable, I have to call getappdata, change the variable, and set it again with setappdata (unless they happen to be classes that inherit from 'handle', in which case it is not necessary to call setappdata to store the new global variable's value).
In other words, handles.figure1 is a surrogate for the global variable data needed by all callbacks.
Is this the most efficient way to share global variables across callbacks? Or is there some better and generally accepted way?

Antworten (1)

Ashish Gudla
Ashish Gudla am 8 Aug. 2014
As you mentioned, there are multiple ways to share data among call backs as explained in this link . If you are storing data as “user data” or “application data” you need to have access to the object handle.
But if you don’t want to use “set/setappdata” and “get/getappdata” functions to access the shared data, the only way would be to make the call back functions nested within your main GUI function. You can refer to the above link for the example.
However this method of sharing data is not recommended if you are developing your GUI using GUIDE as the generated “.m” file does not nest the functions and requires manual modification of the functions.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by