Creating Graphical User Interfaces Previous page   Next Page

Mechanisms for Managing Data

This section explains the three mechanisms that are provided for managing data in the GUI environment. They provide a way for applications to save and retrieve data stored with the GUI.

The GUI data and aplication data mechanisms are similar but GUI data can be simpler to use. You can use either or both in your GUI, whether you create it using GUIDE or create it programmatically. The UserData property can also hold application-defined data.

GUI Data

The guidata function can store a variable as GUI data, or retrieve the value of GUI data. GUI data is always associated with the GUI figure. It is available to all callbacks of all components of the GUI. If you specify a component handle when you save or retrieve GUI data, MATLAB automatically associates the data with the component's parent figure.

GUI data can contain only one variable at any time. Writing GUI data overwrites the existing GUI data. For this reason, GUI data is usually defined to be a structure to which you can add fields as you need them.

GUI data provides application developers with a convenient interface to a figure's application data:

guidata is particularly useful in conjunction with guihandles, which creates a structure containing the handles of all the components in a GUI.

To change GUI data in an M-file not generated by GUIDE:

  1. Get a copy of the data with the command mydata = guidata(object_handle). Although GUI data is associated with the figure, object_handle can be the handle of the figure or any component in the figure.
  2. Make the desired changes to mydata.
  3. Save the changed version of data with the command guidata(object_handle,mydata).

To change GUI data in an M-file generated by GUIDE:

  1. Make the desired changes to the handles structure that is automatically passed as an argument to every callback.
  2. Save the changed version of handles with the command guidata(hObject,handles). The input argument hObject is the handle of the object for which the callback is executing.

Application Data

Application data provides a way for applications to save and retrieve data associated with an object. For a GUI, this is usually the GUI figure object but can also be any component. The data is stored as name/value pairs. Application data enables you to create what are essentially user-defined properties for an object.

The following table summarizes the functions that provide access to application data. For more detailed information, see the individual function reference pages.

Functions for Managing Application Data
Function
Purpose
setappdata
Specify named application data for an object. The object does not have to be a figure. You can specify more than one named application data for an object. However, each name must be unique for that object and can be associated with only one value, usually a structure.
getappdata
Retrieve named application data. To retrieve named application data, you must know the name associated with the application data and the handle of the object with which it is associated.
isappdata
True if the named application data exists.
rmappdata
Remove the named application data.

Note that if you are creating your GUI programmatically, you can store the structure of handles returned by guihandles as application data. You do not have to store it as GUI data. If you are using GUIDE you should always save the handles structure as GUI data.

UserData Property

All GUI components, menus, and the figure have a UserData property. You can assign any valid MATLAB value to the UserData property. To retrieve the data, a callback must know the handle of the component with which the data is associated.


Previous page  Managing Application-Defined Data Adding Fields to a Data Structure Next page

© 1994-2005 The MathWorks, Inc.