Creating Graphical User Interfaces Previous page   Next Page

Sharing Data with the Handles Structure

When you run a GUI, the M-file creates a handles structure that contains all the data for GUI objects, such as controls, menus, and axes. The handles structure is passed as an input to each callback. You can use the handles structure to

Sharing Data

To store data that is contained in a variable X, set a field of the handles structure equal to X and then save the handles structure with the guidata function:

You can retrieve the data in any other callback with the command

For an example of sharing data between callbacks, see Example: Passing Data Between Callbacks.

For more information about handles, see Managing GUI Data with the Handles Structure.

Accessing GUI Data

You can access any of the data for the GUI components from the handles structure. For example, suppose your GUI has a pop-up menu, whose Tag is my_menu, containing three items, whose String properties are chocolate, strawberry, and vanilla. You want another component in the GUI -- a push button, for example -- to execute a command on the currently selected menu item. In the callback for the push button, you can insert the command

The command sets the value of current_choice to chocolate, strawberry, or vanilla, depending on which item is currently selected in the menu.

You can also access the data for the entire GUI from the handles structure. If the figure's Tag is figure1, then

contains the figure's handle. For example, you can make the GUI close itself with the command

For an example of closing a GUI with this command, look at the callback for the Close push button for the template described in GUI with Axes and Menu.


Previous page  Understanding the GUI M-File Functions and Callbacks in the M-File Next page

© 1994-2005 The MathWorks, Inc.