MATLAB Creating Graphical User Interfaces

(ff) #1
% 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');
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);

Notice that hObject is an input argument to the slider1_Callback function.
hObject is always the component that triggers the callback (the slider, in this case).
Thus, set(hObject,'UserData',data), stores the data variable in the
UserData property of the slider.

9 Add code to the push button callback for retrieving the data. Add these commands to
the end of the function, pushbutton1_Callback.


% Get UserData from the slider
data = get(handles.slider1,'UserData');
currentval = data.val;
diffval = data.diffMax;
display([currentval diffval]);

After you add the commands, pushbutton1_Callback looks like this.

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get UserData from the slider
data = get(handles.slider1,'UserData');
currentval = data.val;
diffval = data.diffMax;
display([currentval diffval]);

Share Data Among Callbacks
Free download pdf