User`s guide

13 Manage Application-Defined Data
Slider Values. In this example, both the slider callback, slider_callbac k
and the edit text callback, edittext_callback , retrieve the structure
slider from the e dit text UserD ata property. The slider structure holds
previous and current v alues of the slider. The callbacks then save the value
slider.val to slider.previous_val before retrieving the new value and
assigning it to
slider.val. Before returning, each callback saves the slider
structure in the edit text UserData property.
% Get slider structure from ed it text UserData.
slider = get(eth,'UserData',sli der);
slider.previous_val = slider.va l;
slider.val = str2double(get(hObject,'String'));
...
% Save slider structure in UserData before returning.
set(eth,'UserData',slider)
Both callbacks use the get and set functions to retrieve and save the slider
structure in the edit text UserData property.
Share Data with Application Data
Application data can be associated with any o bject—a component, menu, or
the figure itself. To access application data, a callback must know the name
of the data and the handle of the component in which it is stored. Use the
functions
setappdata, getappdata, isappdata,andrmappdata to manage
application data.
For more information about application data, see “Application Data” on page
13-6.
Application Data Example: Passing Data Between Components
The following code is similar to the previous examples, but uses application
data to initialize and maintain the old and new slider values in the edit text
and slider
Callbacks. It also uses nested functions to provide callbacks with
access to other components’ handles, which the main function defines. Copy
the following code listing, paste it into a new file, and save it in your current
folder or elsewh ere on your path as
slider_gui_appdata.m.Alternatively,
clickheretoplace
slider_gui_appdata.m in your current folder. Run the
function by typing
slider_gui_appdata at the command line.
13-18