Specifications

Managing GUI Data with the Handles Structure
4-13
Retrieving the structure within the subfunction when it is required.
Using the guidata Function without the Handles Structure
The guidata function provides a convenient interface to the figure’s application
data. It enables you to access the data without having to find the figure’s
handle (something that may be difficult when the handle is hidden) and avoids
the need to create and maintain a hard-coded property name for the application
data throughout your source code.
For example, you would set up the code similar to this.
In the initialization code:
fig = openfig(mfilename,'new'); % open GUI and save figure handle
.
.
.
data.field1 = value1;
% Create a structure
guidata(fig,data) % Save the structure
Within a callback subfunction:
data = guidata(gcbo); % Load the data
value1 = data.field1; % Get the stored value
new_value = value1 + 1;
data.field1 = new_value;
% Save a new value
guidata(gcbo,data) % Save the structure
Note that, once a callback routine has begun execution, guidata can obtain the
handle of the figure using
gcbo (the handle of the object whose callback has
been called). However, in the initialization section, no callback routine has
been invoked so you cannot use
gcbo. In this case, you can use the handle of the
GUI figure returned by
openfig.
Note guidata always uses the same property name so you cannot use the
handles structure and define your own application data using guidata. See
Application-Defined Data for information about defining application data.