MATLAB Creating Graphical User Interfaces

(ff) #1

When the user moves the slider, the slider_callback uses these commands to store
data in a structure:



  • data = struct('val',sval,'diffMax',diffMax) stores the values, sval and
    diffMax, in a structure called data.

  • hObject.UserData = data stores the value of data in the UserData property of
    the slider.


When the user clicks the push button, the button_callback uses these commands to
retrieve the data:



  • h = findobj('Tag','slider1') finds the slider component.

  • data = h.UserData gets the value of the slider’s UserData property.


Share UserData in GUIDE Apps


To set up a GUIDE app for sharing slider data with the UserData property, perform these
steps:


1 In the Command Window, type guide.


(^2) In the GUIDE Quick Start dialog box, select Blank GUI (Default). Then, click OK.
(^3) Display the names of the UI components in the component palette:
a Select File > Preferences > GUIDE.
b Select Show names in component palette.
c Click OK.
4 Select the push button tool from the component palette at the left side of the Layout
Editor and drag it into the layout area.
(^5) Select the slider tool from the component palette at the left side of the Layout Editor
and drag it into the layout area.
(^6) Select File > Save. Save the UI as myslider.fig. MATLAB opens the code file in
the Editor.
(^7) Set the initial value of the UserData property in the opening function,
myslider_OpeningFcn. This function executes just before the UI is visible to users.
In myslider_OpeningFcn, insert these commands immediately after the command,
handles.output = hObject.
Share Data Among Callbacks

Free download pdf