User`s guide
Ways to Manage Data in a GUIDE GUI
Adding Fields to an Application Data Structure in GUIDE
Application data is usually defined as a structure. This enables you to
add fields as necessary. In this example, a push button adds a field to the
application data structure
mydata created in the previous section:
1 Use getappdata to retrieve the structure.
The name of the application data structure is
mydata. It is associated with
thefigurewhose
Tag is figu re1.Sinceyoupassthehandles structure to
every callback, the code specifies the figure’s handle as
handles.figure1:
function mygui_ push button1(hObject, ev entd ata, handles)
matrices = getappdata(handles.f igure1,'mydata');
2 Create a new field and assign it a value:
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 command uses setappdata to save
the
matrices structure as the application data structure mydata:
setappdata(handles.figure1,'mydata',mat rices);
GUI Data
GUI data is always associated with the GUI figure and is available to all
callbacks of all the GUI components created in GUIDE . If you specify a
component handle when you save or retrieve GUI data, MA TLAB software
automatically associates the data with the component’s parent figure. 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.
• Youdonotneedtocreateandmaintainahard-codednameforthedata
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
9-7