MATLAB Creating Graphical User Interfaces

(ff) #1

'String','Display Values',...
'Callback',@button_callback);
end


function slider_callback(hObject,eventdata)
diffMax = hObject.Max - hObject.Value;
setappdata(hObject.Parent,'slidervalue',hObject.Value);
setappdata(hObject.Parent,'difference',diffMax);
% For R2014a and earlier:
% maxval = get(hObject,'Max');
% currval = get(hObject,'Value');
% diffMax = maxval - currval;
% parentfig = get(hObject,'Parent');
% setappdata(parentfig,'slidervalue',currval);
% setappdata(parentfig,'difference',diffMax);
end


function button_callback(hObject,eventdata)
currentval = getappdata(hObject.Parent,'slidervalue');
diffval = getappdata(hObject.Parent,'difference');
% For R2014a and earlier:
% parentfig = get(hObject,'Parent');
% currentval = getappdata(parentfig,'slidervalue');
% diffval = getappdata(parentfig,'difference');


display([currentval diffval]);
end


When the user moves the slider, the slider_callback function calculates diffMax.
Then, it uses these commands to modify the application data:



  • setappdata(hObject.Parent,'slidervalue',hObject.Value) stores the
    current slider value in the figure using the name, 'slidervalue'. In this case,
    hObject.Parent is the figure.

  • setappdata(parentfig,'difference',diffMax) stores diffMax in the figure
    using the name, 'difference'.


When the user clicks the push button, the button_callback function retrieves the data
using these commands:



  • currentval = getappdata(hObject.Parent,'slidervalue') retrieves the
    current slider value from the figure. In this case, hObject.Parent is the figure.


Share Data Among Callbacks
Free download pdf