Creating Graphical User Interfaces |
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:
gcbo
. For GUIDE users, the object handle is automatically passed to each callback as hObject
.
guidata
is particularly useful in conjunction with guihandles
, which creates a structure containing the handles of all the components in a GUI.
Note for GUIDE Users
GUIDE uses guidata to store and maintain the handles structure, which contains the handles of all components in the GUI. The GUIDE templates also use the handles structure to store application-defined data. In a GUIDE-generated M-file, do not use guidata to store any data other than handles . If you do, you may overwrite the handles structure and your GUI will not work. If you need to store other data with your GUI, you can add it to the handles structure. For more information, see Managing GUI Data with the Handles Structure and Adding Fields to a Data Structure.
|
To change GUI data in an M-file not generated by GUIDE:
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.
mydata
.
data
with the command guidata(object_handle,mydata)
.
To change GUI data in an M-file generated by GUIDE:
handles
structure that is automatically passed as an argument to every callback.
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.
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.
Managing Application-Defined Data | Adding Fields to a Data Structure |
© 1994-2005 The MathWorks, Inc.