Method Description Requirements and Trade-Offs
“Create Nested
Callback
Functions
(Programmatic
Apps)” on page
11-12
Nest your callback functions inside
your main function. This gives
your callback functions access to
all the variables in the main
function.
- Requires callback functions to
be coded in the same file as the
main function. - Not recommended for GUIDE
apps. - Can share multiple variables.
“Store Data
Using the
guidata
Function” on
page 11-13
Share data with the figure window
using the guidata function.
- Stores or retrieves the data
through any UI component. - Stores only one variable at a
time, but you can store multiple
values as a struct array or
cell array.
Store Data in UserData or Other Object Properties
UI components contain useful information in their properties. For example, you can find
the current position of a slider by querying its Value property. In addition, all
components have a UserData property, which can store any MATLAB variable. All
callback functions can access the value stored in the UserData property as long as those
functions can access the component.
Share UserData in Apps Created Programmatically
Use dot notation, component.propertyname, to get or set property values
programmatically. Dot notation works in R2014b and later releases. This code gets and
sets the name of a figure.
hfig = figure;
figname = hfig.Name;
hfig.Name = 'My Window';
If you are using an earlier release, use the get and set functions instead:
hfig = figure;
figname = get(hfig,'Name');
set(hfig,'Name','My Window');
Share Data Among Callbacks