Creating Graphical User Interfaces Previous page   Next Page

Sharing Data Among a GUI's Callbacks

You can use GUI data, application data, and the UserData property to share data among a GUI's callbacks. See Mechanisms for Managing Data for information about these data forms.

GUI Data

GUI data is accessible to all callbacks of the GUI. A callback for one component can set a value in GUI data which can then be read by a callback for another component. If GUI data is a structure, then both callbacks must know the name of the field that is being used to store the data.

For GUIDE users, arguments that are passed from the command line to the opening function can be made available to the callbacks if the opening function adds them to the handles structure.

Application Data

Application data can be associated with any object. In a GUI, if you asssociate it with the figure, all the GUI's callbacks can easily retrieve it by name, using the figure handle. You can also associate application data with individual components or menus.

To access application data, a calback must know the name of the data and the handle of the component with which it is associated.

Use the functions setappdata, getappdata, isappdata, and rmappdata to manage application data.

UserData Property

Use the get function to retrieve application-defined data from a component's UserData property. You must know the handle of the component to retrieve the data. If you are using GUIDE, you can retrieve the component handle from the handles structure. If you have created your GUI programmatically, you can use the guihandles function to generate a structure of handles for your GUI.

Example: Using GUI Data in a GUIDE-Generated GUI

This example uses GUI data to pass data between a slider and an editable text box in a GUI created using GUIDE. The GUI behavior is as follows:

The following figure shows the GUI with a static text field above the edit text box.

GUI with static text field: Enter a value or click the slider

Defining the Data Fields in the Opening Function.   The GUI records the number of times a user enters an erroneous value in the text box and stores this number in a field of the handles structure. Start by defining this field, called number_errors, in the opening function as follows:

Type this command before the following line, which GUIDE automatically inserts into the opening function.

The guidata command saves the handles structure so that it can be retrieved in the callbacks.

Setting the Edit Text Value from the Slider Callback.   The following command in the slider callback updates the value displayed in the edit text when a user moves the slider and releases the mouse button.

The code combines three commands:

Setting the Slider Value from the Edit Text Callback.   The edit text callback sets the slider's value to the number the user types in, after checking to see if it is a single numeric value between 0 and 1. If the value is out of range, then the error count is incremented and the edit text displays a message telling the user how many times they have entered an invalid number.

If the user types a number between 0 and 1 in the edit box and then presses Enter or clicks outside the edit box, the callback sets handles.slider1 to the new value and the slider moves to the corresponding position.

If the entry is invalid -- for example, 2.5 -- the GUI increments the value of handles.number_errors and displays a message like the following:

Message: You have entered an invalid entry 3 times


Previous page  Adding Fields to a Data Structure Properties That Control Component Behavior Next page

© 1994-2005 The MathWorks, Inc.