MATLAB Creating Graphical User Interfaces

(ff) #1
setappdata(handles.figure1,'difference',1);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes junk wait for user response (see UIRESUME)
% 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. In this case, handles.figure1 is the
figure object. Thus, setappdata can use this figure object to store the data.

(^8) Add code to the slider callback for changing the data. Add these commands to the
end of the function, slider1_Callback.
maxval = get(hObject,'Max');
currval = get(hObject,'Value');
diffMax = maxval - currval;
% Store application data
setappdata(handles.figure1,'slidervalue',currval);
setappdata(handles.figure1,'difference',diffMax);
After you add the commands, slider1_Callback looks like this.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
maxval = get(hObject,'Max');
currval = get(hObject,'Value');
diffMax = maxval - currval;
% Store application data
setappdata(handles.figure1,'slidervalue',currval);
setappdata(handles.figure1,'difference',diffMax);
This callback function has access to the handles structure, so the setappdata
commands store the data in handles.figure1.
(^9) Add code to the push button callback for retrieving the data. Add these commands to
the end of the function, pushbutton1_Callback.
Share Data Among Callbacks

Free download pdf