Creating Graphical User Interfaces Previous page   Next Page

Sharing Data Between Callbacks

This topic describes the process for sharing data between callbacks in a GUI. Subsequent topics, Adding Code to the Opening Function and Adding Code to the Callbacks, contain examples.

You can share data between callbacks by storing the data in the MATLAB handles structure. All components in a GUI share the same handles structure. It is passed as an input argument to all callbacks generated by GUIDE.

For example, to store data contained in vector X in the handles structure, you

  1. Choose a name for the field of the handles structure where you want to store the data, for example, handles.my_data
  2. Add the field to the handles structure and set it equal to X with the following statement:
  3. Save the handles structure with the guidata function:

Here, hObject is the handle to the component object that executes the callback. The component's object handle is passed as the input argument, hObject, to each of its callbacks that is generated by GUIDE.

To retrieve X in another callback, use the command

You can access the data in the handles structure in any callback because hObject and handles are input arguments for all the callbacks generated by GUIDE.

For more detailed information on the handles structure, see Managing GUI Data with the Handles Structure.


Previous page  Opening the GUI M-File Adding Code to the Opening Function Next page

© 1994-2005 The MathWorks, Inc.