Many apps contain interdependent controls, menus, and graphics objects. Since each callback function has its own scope, you must explicitly share data with those parts of your app that need to access it. The table below describes several different methods for sharing data within your app.
Method | Description | Requirements and Trade-Offs |
---|---|---|
Store Data in UserData or Other Object Properties | Get or set property values directly through the component object. All UI components have a
|
|
Store Data as Application Data | Associate data with a specific component using the
|
|
Create Nested Callback Functions | Nest your callback functions inside your main function. This gives your callback functions access to all the variables in the main function. |
|
Store Data Using the guidata Function | Share data with the figure window using the |
|
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.
Use dot notation to set and query properties of the traditional figure.
hfig = figure; figname = hfig.Name; hfig.Name = 'My Window';
If your code does not have direct access to a component, use the findobj
function to search for
that component. If the search is successful, findobj
returns the component as output. Then you can access the component’s
properties.
The following app code uses the UserData
property to
share information about the slider. To see how it works, copy and paste this
code into an editor and run it.
function my_slider() hfig = figure(); slider = uicontrol('Parent', hfig,'Style','slider',... 'Units','normalized',... 'Position',[0.3 0.5 0.4 0.1],... 'Tag','slider1',... 'UserData',struct('val',0,'diffMax',1),... 'Callback',@slider_callback); button = uicontrol('Parent', hfig,'Style','pushbutton',... 'Units','normalized',... 'Position',[0.4 0.3 0.2 0.1],... 'String','Display Difference',... 'Callback',@button_callback); end function slider_callback(hObject,eventdata) sval = hObject.Value; diffMax = hObject.Max - sval; data = struct('val',sval,'diffMax',diffMax); hObject.UserData = data; end function button_callback(hObject,eventdata) h = findobj('Tag','slider1'); data = h.UserData; display([data.val data.diffMax]); end
slider_callback
uses these
commands to store data in a structure:
data = struct('val',sval,'diffMax',diffMax)
stores the values, sval
and
diffMax
, in a structure called
data
.
hObject.UserData = data
stores the value of
data
in the UserData
property of the slider.
When the user clicks the push button, the button_callback
uses these commands to retrieve the data:
h = findobj('Tag','slider1')
finds the slider
component.
data = h.UserData
gets the value of the
slider’s UserData
property.
To store application data, call the setappdata
function:
setappdata(obj,name,value);
obj
, is the component object in which to store the data.
The second input, name
, is a friendly name that describes the
value. The third input, value
, is the value you want to
store.To retrieve application data, use the getappdata
function:
data = getappdata(obj,name);
obj
, must be the component object containing the data. The
second input, name
, must match the name you used to store the
data. Unlike the UserData
property, which only holds only one
variable, you can use setappdata
to store multiple
variables.This app uses application data to share two values. To see how it works, copy and paste this code into an editor and run it.
function my_slider() hfig = figure(); setappdata(hfig,'slidervalue',0); setappdata(hfig,'difference',1); slider = uicontrol('Parent', hfig,'Style','slider',... 'Units','normalized',... 'Position',[0.3 0.5 0.4 0.1],... 'Tag','slider1',... 'Callback',@slider_callback); button = uicontrol('Parent', hfig,'Style','pushbutton',... 'Units','normalized',... 'Position',[0.4 0.3 0.2 0.1],... 'String','Display Values',... 'Callback',@button_callback); end function slider_callback(hObject,eventdata) diffMax = hObject.Max - hObject.Value; setappdata(hObject.Parent,'slidervalue',hObject.Value); setappdata(hObject.Parent,'difference',diffMax); end function button_callback(hObject,eventdata) currentval = getappdata(hObject.Parent,'slidervalue'); diffval = getappdata(hObject.Parent,'difference'); display([currentval diffval]); end
slider_callback
function
calculates diffMax
. Then, it uses these commands to modify
the application data:
setappdata(hObject.Parent,'slidervalue',hObject.Value)
stores the current slider value in the figure using the name,
'slidervalue'
. In this case,
hObject.Parent
is the figure.
setappdata(parentfig,'difference',diffMax)
stores diffMax
in the figure using the name,
'difference'
.
When the user clicks the push button, the button_callback
function retrieves the data using these commands:
currentval =
getappdata(hObject.Parent,'slidervalue')
retrieves the
current slider value from the figure. In this case,
hObject.Parent
is the figure.
diffval =
getappdata(hObject.Parent,'difference')
retrieve the
difference value from the figure.
You can nest callback functions inside the main function of a programmatic app. When you do this, the nested callback functions share a workspace with the main function. As a result, the nested functions have access to all the UI components and variables defined in the main function. The following example code uses nested functions to share data about the slider position. To see how it works, copy and paste this code into an editor and run it.
function my_slider() hfig = figure(); data = struct('val',0,'diffMax',1); slider = uicontrol('Parent', hfig,'Style','slider',... 'Units','normalized',... 'Position',[0.3 0.5 0.4 0.1],... 'Tag','slider1',... 'Callback',@slider_callback); button = uicontrol('Parent', hfig,'Style','pushbutton',... 'Units','normalized',... 'Position',[0.4 0.3 0.2 0.1],... 'String','Display Difference',... 'Callback',@button_callback); function slider_callback(hObject,eventdata) sval = hObject.Value; diffMax = hObject.Max - sval; data.val = sval; data.diffMax = diffMax; end function button_callback(hObject,eventdata) display([data.val data.diffMax]); end end
struct
array called
data
. When the user moves the slider, the
slider_callback
function updates the val
and diffMax
fields of the data
structure. When
the end user clicks the push button, the button_callback
function
displays the values stored in data
.The guidata
function provides a way to
share data with the figure window. You can store or retrieve your data in any
callback through the hObject
component. This means that, unlike
working with UserData
or application data, you do not need
access to one specific component to set or get the data. Call
guidata
with two input arguments to store
data:
guidata(object_handle,data);
object_handle
, is any UI component (typically
hObject
). The second input, data
, is the
variable to store. Every time you call guidata
using two input
arguments, MATLAB overwrites any previously stored data. This means you can only store
one variable at a time. If you want to share multiple values, then store the data as
a struct
array or cell array.To retrieve data, call guidata
using one input argument and
one output
argument:
data = guidata(object_handle);
If your data is stored as a struct
array or cell array, and you
want to update one element without changing the other elements, then retrieve the
data and replace it with the modified
array:
data = guidata(hObject); data.myvalue = 2; guidata(hObject,data);
guidata
to Store and Share DataTo use guidata
in a programmatic app,
store the data with some initial values in the main function. Then you can
retrieve and modify the data in any callback function.
The following code is a simple example of a programmatic app that uses
guidata
to share a structure containing two fields. To
see how it works, copy and paste this code into an editor and run
it.
function my_slider() hfig = figure(); guidata(hfig,struct('val',0,'diffMax',1)); slider = uicontrol('Parent', hfig,'Style','slider',... 'Units','normalized',... 'Position',[0.3 0.5 0.4 0.1],... 'Tag','slider1',... 'Callback',@slider_callback); button = uicontrol('Parent', hfig,'Style','pushbutton',... 'Units','normalized',... 'Position',[0.4 0.3 0.2 0.1],... 'String','Display Values',... 'Callback',@button_callback); end function slider_callback(hObject,eventdata) data = guidata(hObject); data.val = hObject.Value; data.diffMax = hObject.Max - data.val; guidata(hObject,data); end function button_callback(hObject,eventdata) data = guidata(hObject); display([data.val data.diffMax]); end
slider_callback
function
executes these commands to retrieve and modify the stored data:data = guidata(hObject)
retrieves the stored data
as a structure.
data.diffMax = maxval - data.val
modifies the
diffMax
field in the structure.
guidata(hObject,data)
stores the modified
structure.
When the user clicks the push button, the button_callback
function calls guidata
to retrieve a copy of the stored
structure. Then it displays the two values stored in the structure.