MATLAB Creating Graphical User Interfaces

(ff) #1

Share Data Among Callbacks


In this section...
“Overview of Data Sharing Techniques” on page 11-2
“Store Data in UserData or Other Object Properties” on page 11-3
“Store Data as Application Data” on page 11-8
“Create Nested Callback Functions (Programmatic Apps)” on page 11-12
“Store Data Using the guidata Function” on page 11-13
“GUIDE Example: Share Slider Data Using guidata” on page 11-16
“GUIDE Example: Share Data Between Two Apps” on page 11-16
“GUIDE Example: Share Data Among Three Apps” on page 11-17

Overview of Data Sharing Techniques


Many apps contain interdependent controls, menus, and graphics objects. Since each
callback function has its own scope, you must explicitly share data with those parts of
your app that need to access it. The table below describes several different methods for
sharing data within your app.

Method Description Requirements and Trade-Offs
“Store Data in
UserData or
Other Object
Properties” on
page 11-3

Get or set property values directly
through the component object.

All UI components have a
UserData property that can store
any MATLAB data.


  • Requires access to the
    component to set or retrieve
    the properties.

  • UserData holds only one
    variable at a time, but you can
    store multiple values as a
    struct array or cell array.
    “Store Data as
    Application
    Data” on page
    11-8


Associate data with a specific
component using the setappdata
function. You can access it later
using the getappdata function.


  • Requires access to the
    component to set or retrieve
    the application data.

  • Can share multiple variables.


11 Manage Application-Defined Data

Free download pdf