|On this page…|
Most GUIs generate or use data specific to the application. GUI components often need to communicate data to one another and several basic mechanism serve this need.
Although many GUIs are single figures, you can make several GUIs work together if your application requires more than a single figure. For example, your GUI could require several dialog boxes to display and obtain some of the parameters used by the GUI. Your GUI could include several individual tools that work together, either at the same time or in succession. To avoid communication via files or workspace variables, you can use any of the methods described in the following table.
|Data-Sharing Method||How it Works||Use for...|
|Property/value pairs||Send data into a newly invoked or existing GUI by passing it along as input arguments.||Communicating data to new GUIs|
|Output||Return data from the invoked GUI.||Communicating data back to the invoking GUI, such as passing back the handles structure of the invoked GUI|
|Function handles or private data||Pass function handles or data through one of the four following methods:||Exposing functionality within a GUI or between GUIs|
|Nested Functions: share the name space of all superior functions||Accessing and modifying variables defined in a directly or indirectly enclosing function, typically within a single GUI figure|
|UserData: Store data in this figure or component property. Communicate to other GUIs via handle references.||Communicating data within a GUI or between GUIs; UserData is limited to one variable, often supplied as a struct|
|Application Data (getappdata / setappdata, ...): Store named data in a figure or component. Communicate to other GUIs via handle references.||Communicating data within a GUI or between GUIs; any number or types of variables can be stored as application data through this API|
|guidata: Store data in the handles structure of a GUI. Communicate to other GUIs via handle references.||Communicating data within a GUI or between GUIs—a convenient way to manage application data. GUI Data is a struct associated with the GUI figure.|
The next three sections describe mechanisms that provide a way to manage application-defined data, stored within a GUI:
Nested Functions — Share variables defined at a higher level and call one another when called function is below above, or a sibling of the caller.
UserData Property — A MATLAB® workspace variable that you assign to GUI components and retrieve like any other property.
Application Data — Provides a way for applications to save and retrieve data associated with a specified object. For a GUI, this is usually the GUI figure, but it 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.
GUI Data — Uses the guidata function to manage GUI data. This function can store a single variable as GUI data in a MATLAB structure, which in GUIDE is called the handles structure. You use the function to retrieve the handles structure as well as to update it.
You can compare the three approaches applied to a simple working GUI in Examples of Sharing Data Among a GUI's Callbacks.
When you place nested functions in a GUI code file, they enable callback functions to share data freely without it having to be passed as arguments:
The callbacks and utility functions automatically have access to the data and the component handles because they are defined at a higher level. Using this approach can eliminate the need for storing UserData, application data, or GUI Data in many instances.
Note For the rules and restrictions that apply to using nested functions, see Nested Functions.
See Share Data with Nested Functions for a complete example.
All GUI components, including menus and the figure itself have a UserData property. You can assign any valid MATLAB workspace value as the UserData property's value, but only one value can exist at a time. To retrieve the data, a callback must know the handle of the component in which the data is stored. You access UserData using get and set with the appropriate object's handle. The following example illustrates this pattern:
function edittext1_callback(hObject,eventdata) mystring = get(hObject,'String'); set(hObject,'UserData',mystring);
function pushbutton1_callback(hObject,eventdata) string = get(edittexthandle,'UserData');
For example, if the menu item is Undo, its code could reset the String of edittext1 back to the value stored in its UserData. To facilitate undo operations, the UserData can be a cell array of strings, managed as a stack or circular buffer.
Specify UserData as a structure if you want to store multiple variables. Each field you define can hold a different variable.
Note To use hObject (the calling object's handle), you must specify a component's callback properties as function handles rather than as strings or function names. When you do, the component handle is automatically passed to each callback as hObject. See Specify Callbacks in Function Calls for more information.
See Share Data with UserData for a complete example.
Application data is data that is meaningful to or defined by your application. You attach application data to a figure or any GUI component (other than ActiveX® controls) with the functions setappdata and getappdata, The main differences between it and UserData are:
You can assign multiple values to application data, but only one value to UserData.
Your code must reference application data by name (like using a Tag), but can access UserData like any other property
Only Handle Graphics® MATLAB objects use this property. The following table summarizes the functions that provide access to application data. For more details, see the individual function reference pages.
Functions for Managing Application Data
Specify named application data for an object (a figure or other Handle Graphics object in your GUI). You can specify more than one named application data item per object. However, each name must be unique for that object and can be associated with only one value, usually a structure.
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. If you specify a handle only, all the object's application data is returned.
True if the named application data exists on the specified object.
Remove named application data from the specified object.
Use the setappdata function to create application data. This example generates a 35-by-35 matrix of normally distributed random numbers and creates application data mydata, associated with the figure, to manage it.
matrices.rand_35 = randn(35); setappdata(figurehandle,'mydata',matrices);
Application data is usually defined as a structure to enable you to add fields as necessary. This example adds a field to the application data structure mydata created in the previous topic:
From the example in the previous topic, the name of the application data structure is mydata. It is associated with the figure.
matrices = getappdata(figurehandle,'mydata');
matrices.randn_50 = randn(50);
adds the field randn_50 to the matrices structure and sets it to a 50-by-50 matrix of normally distributed random numbers.
A callback can retrieve (and modify) this application data in the same manner, but needs to know what the figure handle is to access it. By using nested functions and creating the figure at the top level, the figure handle is accessible to all callbacks and utility functions nested at lower levels. For information about using nested functions, see Nested Functions. See Share Data with Application Data for a complete example of using application data.
Most GUIs generate or use data that is specific to the application. These mechanisms provide a way for applications to save and retrieve data stored with the GUI. With GUI data:
You can access the data from within a callback routine using the component's handle, without needing to find the figure handle.
You do not need to create and maintain a hard-coded name for the data throughout your source code.
Use the guidata function to manage GUI data. This function can store a single variable as GUI data. GUI data differs from application data in that
GUI data is a single variable; however, when defined as a structure, you can add and remove fields.
Application data can consist of many variables, each stored under a separate unique name.
You access GUI data using the guidata function, which both stores and retrieves GUI data.
Whenever you use guidata to store GUI data, it overwrites the existing GUI data.
Using the getappdata, setappdata, and rmappdata functions does not affect 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 software automatically associates the data with the component's parent figure.
GUI data can contain only one variable at any time. Writing GUI data with a different variable 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.
You can access the data from within a callback routine using the component's handle, without having to find the figure handle. If you specify a component's callback properties as function handles, the component handle is automatically passed to each callback as hObject. See Specify Callbacks in Function Calls for more information.
Because there can be only one GUI data variable and it is associated with the figure, you do not need to create and maintain a hard-coded name for the data throughout your source code.
Note GUIDE uses GUI data to store its handles structure, and includes it as an argument (called handles) in every callback . Programmatic GUI callbacks do not include GUI Data, but any callback function can access it from its component's handle (hObject, the first callback argument). If GUIDE originally created your GUI, see Changing GUI Data in a Code File Generated by GUIDE.
mydata.iteration_counter = 0; mydata.number_errors = 0;
mydata = guidata(hObject); % Get the GUI data. mydata.iteration_counter = mydata.iteration_counter +1; guidata(hObject,mydata); % Save the GUI data.
Note To use hObject, you must specify a component's callback properties as function handles. When you do, the component handle is automatically passed to each callback as hObject. See Specify Callbacks in Function Calls for more information.
To add a field to a GUI data structure:
mydata = guidata(hObject)
mydata.iteration_state = 0;
adds the field iteration_state to the structure mydata and sets it to 0.
where hObject is the handle of the component for which the callback was triggered. MATLAB software associates a new copy of the mydata structure with the component's parent figure.
See Share Data with GUI Data for a complete example.