Creating Graphical User Interfaces |
Callbacks in Programmatically Created GUIs
Although you may eventually want to define your own syntactical conventions, the following provides a good place to start.
Function Definition
Your function definition could look something like this:
% Description of event that triggers callback execution. function pushbutton1_Callback(source, eventdata, handles,...) ...
The comment line describes the event that triggers execution of the callback.
To help you remember the component and callback property with which this callback is associated, construct the callback name by appending an underscore (_) and the name of the callback property to the component's Tag
property. In the example above, pushbutton1
is the Tag
property for the push button, and Callback
is one of the push button's callback properties. See Associating Callbacks with Components for more information.
Input Arguments
If you use a handle to specify the callback in the component callback property, i.e., @pushbutton_callback
as shown here
MATLAB automatically passes the handle of the component for which the event was triggered and eventdata
as the first two arguments of the callback. The function definition line for your callback must account for these two arguments. The second element of the cell array, handles
in the example above, becomes the third argument of the callback. For example,
The input arguments are defined as:
source
-- Handle of the object for which the callback was triggered.
eventdata
-- Reserved for later use.
Note
Three eventdata fields are defined for use with button groups. These fields enable you to determine the previous and current radio or toggle button selections maintained by the buttoon group. See SelectionChangeFcn in the Uibuttongroup Properties for more information.
|
handles
-- Structure you create with the guihandles
function, prior to setting the component callback properties, that contains the handles of all the objects in the figure. It may also contain application-defined data.
Note If you are using nested functions, you do not need this argument to access the component handles. See Files for Programmatically Created GUIs for more information. |
For a GUI that contains an edit text, a uipanel, a popup menu, and a push button, the handles
structure you create with guihandles
looks similar to this. guihandles
uses the component Tag
properties to name the elements of the structure.
If you make any changes to this structure, you must explicitly save it before exiting the callback. To do this, you can use either guidata
or setappdata
. See Managing Application-Defined Data for information about using these functions.
Callback Conventions in GUIDE | Initialization Callbacks in GUIDE |
© 1994-2005 The MathWorks, Inc.