This code uses the handles structure to access the slider. The command, data =
get(handles.slider1,'UserData'), gets the slider’s UserData property. Then,
the display function displays the stored values.
(^10) Save your code by pressing Save in the Editor Toolstrip.
Store Data as Application Data
To store application data, call the setappdata function:
setappdata(obj,name,value);
The first input, 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);
The component, 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.
Share Application Data in Apps Created Programmatically
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],...
11 Manage Application-Defined Data