% Retrieve application data
currentval = getappdata(handles.figure1,'slidervalue');
diffval = getappdata(handles.figure1,'difference');
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)
% Retrieve application data
currentval = getappdata(handles.figure1,'slidervalue');
diffval = getappdata(handles.figure1,'difference');
display([currentval diffval]);
This callback function has access to the handles structure, so the getappdata
commands retrieve the data from handles.figure1.
(^10) Save your code by pressing Save in the Editor Toolstrip.
Create Nested Callback Functions (Programmatic Apps)
You can nest callback functions inside the main function of a programmatic app. When
you do this, the nested callback functions share a workspace with the main function. As a
result, the nested functions have access to all the UI components and variables defined in
the main function. The following example code uses nested functions to share data about
the slider position. To see how it works, copy and paste this code into an editor and run it.
function my_slider()
hfig = figure();
data = struct('val',0,'diffMax',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],...
'String','Display Difference',...
11 Manage Application-Defined Data