User`s guide

Ways to Manage Data in a Programmatic GUI
Create 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',ma trices);
Add Fields to an Application Da ta 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.
Fromtheexampleintheprevioustopic,thenameoftheapplicationdata
structure is
mydata. It is associated w ith 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 matr ices 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',matric es);
A callback can retrieve (and modify) this application data in the same
manner,butneedstoknowwhatthefigurehandleistoaccessit. Byusing
nested functions and creating the figure at the top level, the figure handle
is accessible to all callbacks and utility functions nes te d at lowe r levels. For
information about using nested functions, see “Nested Functions”. See “Share
Data with Application Data” on page 13-18 for a complete example of using
application data.
13-7