data = struct('val',0,'diffMax',1);
set(handles.slider1,'UserData',data);
After you add the commands, myslider_OpeningFcn looks like this.
function myslider_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to junk (see VARARGIN)
% Choose default command line output for myslider
handles.output = hObject;
data = struct('val',0,'diffMax',1);
set(handles.slider1,'UserData',data);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myslider wait for user response
% uiwait(handles.figure1);
Notice that handles is an input argument to myslider_OpeningFcn. The handles
variable is a structure that contains all the components in the UI. Each field in this
structure corresponds to a separate component. Each field name matches the Tag
property of the corresponding component. Thus, handles.slider1 is the slider
component in this UI. The command, set(handles.slider1,'UserData',data)
stores the variable, data, in the UserData property of the slider.
(^8) Add code to the slider callback for modifying the data. Add these commands to the
end of the function, slider1_Callback.
maxval = get(hObject,'Max');
sval = get(hObject,'Value');
diffMax = maxval - sval;
data = get(hObject,'UserData');
data.val = sval;
data.diffMax = diffMax;
% Store data in UserData of slider
set(hObject,'UserData',data);
After you add the commands, slider1_Callback looks like this.
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
11 Manage Application-Defined Data