Mechanisms for Managing Data

Nested Functions

Use nested function to create your GUI M-files. They enable callback functions to share data freely without it having to be passed as arguments.

  1. Construct components, define variables, and generate data in the initialization segment of your code.

  2. Nest the GUI callbacks and utility functions at a level below the initialization.

The callbacks and utility functions automatically have access to the data and the component handles because they are defined at a higher level.

GUI 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.

The GUI data and application data mechanisms are similar, but GUI data can be simpler to use. The figure and component UserData properties can also hold application-defined data.

GUI data is managed using the guidata function. This function can store a single variable as GUI data. It is also used to retrieve the value of that variable.

About 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 Providing Callbacks for Components 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.

Creating and Updating GUI Data

  1. Create a structure and add to it the fields you want. For example,

    mydata.iteration_counter = 0;
    mydata.number_errors = 0;
  2. Save the structure as GUI data. MATLAB software associates GUI data with the figure, but you can use the handle of any component in the figure to retrieve or save it.

    guidata(figurehandle,mydata);
  3. To change GUI data from a callback, get a copy of the structure, update the desired field, and then save the GUI data.

    mydata = guidata(hObject);      % Get the GUI data.
    mydata.iteration_counter = mydata.iteration_counter +1;
    guidata(hObject,mydata);        % Save the GUI data.

Adding Fields to a GUI Data Structure

To add a field to a GUI data structure:

  1. Get a copy of the structure with a command similar to the following where hObject is the handle of the component for which the callback was triggered.

    mydata = guidata(hObject)
  2. Assign a value to the new field. This adds the field to the structure. For example,

    mydata.iteration_state = 0;

    adds the field iteration_state to the structure mydata and sets it to 0.

  3. Use the following command to save the data.

    guidata(hObject,mydata)

    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.

Application Data

Application data is data that is meaningful to or defined by your application which you attach to a figure or any GUI component (other than ActiveX controls) through its AppData property. Only Handle Graphics MATLAB objects use this property.

Application data provides a way for applications to save and retrieve data associated with a specified object. 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 (a figure or other Handle Graphics object in your GUI). 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 on the specified object.

rmappdata

Remove named application data from the specified object.

Creating Application Data

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);

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 in the MATLAB Programming Fundamentals documentation.

Adding Fields to an Application Data Structure

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.

  1. Use getappdata to retrieve the structure.

    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');
  2. Create a new field and assign it a value. For example

    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.

  3. Use setappdata to save the data. This example uses setappdata to save the matrices structure as the application data structure mydata.

    setappdata(figurehandle,'mydata',matrices);

UserData Property

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

  1. In this example, an edit text component stores the user-entered string in its UserData property.

    function edittext1_callback(hObject,eventdata)
    mystring = get(hObject,'String');
    set(hObject,'UserData',mystring);
  2. A push button retrieves the string from the edit text component UserData property.

    function pushbutton1_callback(hObject,eventdata)
    string = get(edittexthandle,'UserData');

Specify UserData as a structure if you want to store multiple fields.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS