Callback Syntax and Arguments

Callback Templates

GUIDE defines conventions for callback syntax and arguments and implements these conventions in the callback templates it adds to the M-file. Each template is similar to this one for the Callback subfunction for a push button.

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

...

The first comment line describes the event that triggers execution of the callback. This is followed by the function definition line. The remaining comments describe the input arguments. Insert your code after the last comment.

Certain figure and GUI component callbacks provide event-specific data in the eventdata argument. As an example, this is the template for a push button KeyPressFcn callback.

% --- Executes on key press with focus on pushbutton1
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	 Key: name of the key that was pressed, in lower case
%	 Character: character interpretation of the key(s) that was pressed
%	 Modifier: name(s) of the modifier key(s)(i.e., control, shift) 
% pressed
% handles structure with handles and user data (see GUIDATA)

Callbacks that provide event data and the components to which they apply are listed in the following table. See the appropriate property reference pages for detailed information.

GUI ComponentCallbacks with Event DataProperty Reference Pages
Figure

KeyPressFcn, KeyReleaseFcn, WindowKeyPressFcn, WindowKeyReleaseFcn, WindowScrollWheel

Figure Properties
User interface control (uicontrol)

KeyPressFcn

Uicontrol Properties
Button group (uibuttongroup)

SelectionChangeFcn

Uibuttongroup Properties
Table (uitable)

CellEditCallback, CellSelectionCallback

Uitable Properties

Naming of Callback Functions

The previous callback example includes the following function definition:

function pushbutton1_Callback(hObject,eventdata,handles)

When GUIDE generates the template, it creates 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. The Tag property uniquely identifies a component within the GUI.

The first time you save the GUI after adding a component, GUIDE adds callbacks for that component to the M-file and generates the callback names using the current value of the Tag property. If you change the default Tag for any component, make sure you have not duplicated any other component's Tag value before you save your GUI. GUIDE issues a warning if it determines that duplicate tags exist.

See Changing Callbacks Assigned by GUIDE and Associating Callbacks with Components for more information.

Callback Function Signatures

A function signature itemizes a function's name, the number, order, and types of its parameters, and any qualifiers that apply to the function. When you use the Property Inspector to view a component of a GUI that you have saved at least once, you see that its Callback property is already set. When GUIDE saves a GUI, it

The component may have other callbacks, for example a CreateFcn or a DeleteFcn, which GUIDE populates the same way. It is up to you to add code to the template to make a callback do something.

For example, if you click the pencil-and-paper icon for a push button's Callback property in the Property Inspector, GUIDE presents the GUI's M-file in the MATLAB Editor and positions the cursor at the first line of the callback. When GUIDE defines the function in the M-file as:

function pushbutton1_Callback(hObject, eventdata, handles)

then the function signature for the Callback property, shown in the Property Inspector, is

@(hObject,eventdata)mygui('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

The syntax @(hObject,eventdata) indicates that this is an anonymous function. The signature enables MATLAB to execute the right callback when the user clicks this push button by providing the following information.

The following figure illustrates how these elements relate to one another.

See Input Arguments for details about GUIDE-generated callbacks.

Changing Callbacks Assigned by GUIDE

As described in Naming of Callback Functions, GUIDE generates a name for a callback by concatenates the component's Tag property (checkbox1) and its callback type. Although you cannot change a callback's type, you can change its Tag, which will change the callback's name the next time you save the GUI.

Change a component's Tag property to give its callbacks more meaningful names; for example, you might change the Tag property from checkbox1 to warnbeforesave. If possible, change the Tag property before saving the GUI to cause GUIDE to automatically create callback templates having names you prefer. However, if you decide to change a Tag property after saving the GUI, GUIDE updates the following items according to the new Tag, provided that all components have distinct tags:

To rename a particular callback function without changing the Tag property,

After you alter a callback signature, whenever you click its pencil-and-paper icon to go to the function definition in the GUI M-file, GUIDE presents a dialog box for you to confirm the changes you made.

Click Yes to revert to the GUIDE auto-generated callback. click No to keep the modified callback.

Input Arguments

All callbacks in a GUIDE-generated GUI M-file have the following standard input arguments:

Object Handle

The first argument is the handle of the component issuing the callback. Use it to obtain relevant properties that the callback code uses and change them as necessary. For example,

theText = get(hObject,'String');

places the String property (which might be the contents of static text or name of a button) into the local variable theText. You can change the property by setting it, for example

set(hObject,'String',date)

This particular code changes the text of the object to display the current date.

Event Data

Event data is a stream of data describing user gestures, such as key presses, scroll wheel movements, and mouse drags. The auto-generated callbacks of GUIDE GUIs can access event data for Handle Graphics® and uicontrol and uitable object callbacks. The following ones receive event data when triggered:

Event data is passed to GUIDE-generated callbacks as the second of three standard arguments. For components that issue no event data the argument is empty. For those that provide event data, the argument contains a structure, which varies in composition according to the component that generates it and the type of event.

For example, the event data for a key-press provides information on the key(s) currently being pressed. Here is a GUIDE-generated KeyPressFcn callback template:

% --- Executes on key press with focus on checkbox1 and none of its controls.
function checkbox1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	         Key: name of the key that was pressed, in lower case
%	   Character: character interpretation of the key(s) that was pressed
%	    Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

The eventdata structure passed in has three fields, identifying the Character being pressed (such as '='), the key Modifier (such as 'control'), and the Key name (spelled out, such as 'equals').

Components that provide event data use different structures with event-specific field names to pass data. Callbacks with event data usually are repeatedly issued as long as the event persists or sometimes at the beginning of an event and thereafter only when its values change.

Learn how callbacks use event data by looking at the GUIDE uitable example GUI to Interactively Explore Data in a Table and the programmatic uitable example GUI that Displays and Graphs Tabular Data.

handles Structure

GUIDE creates a handles structure that contains the handles of all the objects in the figure. For a GUI that contains an edit text, a panel, a pop-up menu, and a push button, the handles structure originally looks similar to this. GUIDE uses each component's Tag property to name the structure element for its handle.

handles = 
        figure1: 160.0011
          edit1: 9.0020
       uipanel1: 8.0017
     popupmenu1: 7.0018
    pushbutton1: 161.0011
         output: 160.0011

GUIDE creates and maintains the handles structure as GUI data. It is passed as an input argument to all callbacks and enables a GUI's callbacks to share property values and application data.

For information about GUI data, see Mechanisms for Managing Data and the guidata reference page.

For information about adding fields to the handles structure and instructions for correctly saving the structure, see Managing Application-Defined Data.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS