MATLAB Creating Graphical User Interfaces

(Barry) #1

12 Manage Application-Defined Data


Share Data Among Callbacks


In this section...
“Overview of Data Sharing Techniques” on page 12- 2
“Store Data in UserData or Other Object Properties” on page 12- 3
“Store Data as Application Data” on page 12- 4
“Create Nested Callback Functions (Programmatic UIs)” on page 12- 5
“Store Data Using the guidata Function” on page 12- 6
“Sharing Data Among Multiple GUIDE UIs” on page 12- 9

Overview of Data Sharing Techniques


Many UIs 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 UI that need to access it. The table below describes several different methods for
sharing data within your UI.

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

Use dot syntax,
object.property, to store or
retrieve the value of an object
property.

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
12-4

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.
    “Create Nested
    Callback
    Functions
    (Programmatic


Nest your callback functions
inside your main function. This
gives your callback functions
access to all the variables in the
main function.


  • Requires callback functions to
    be coded in the same file as the
    main function.

  • Not recommended for GUIDE
    UIs.

Free download pdf