12 Manage Application-Defined Data
end
function button_callback(hObject,eventdata)
h = findobj('Tag','slider1');
data = h.UserData;
display([data.val data.diffMax]);
end
When the user moves the slider, the slider_callback function stores the current
slider value and the difference between the current and maximum values in the
UserData property. When the user clicks the push button, the button_callback
function finds the component containing the Tag property, 'slider1'. Then, the last
two commands in the button_callback function get and display the values stored in
the slider’s UserData property, respectively.
You can store any MATLAB variable in the UserData property. The syntax for storing
and retrieving data is the same for GUIDE and programmatic UIs.
Store Data as Application Data
You can share data using application data. To store application data, call the
setappdata function:
setappdata(obj,name,value);
The first input, obj, is the graphics object in which to store the data. The second input,
name, is a string that identifies 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);
The component, obj, must be the graphics object containing the data. The second input,
name, must match the string you used to store the data. Unlike UserData, which only
holds only one variable, you can use setappdata to store multiple variables.
The following programmatic UI code 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);