UI and graphics components have certain properties that you
can associate with specific callback functions. Each of these properties
corresponds to a specific user action. For example, a uicontrol has
a property called
Callback. You can set the value
of this property to be a handle to a callback function, an anonymous
function, or a string containing MATLAB® commands. Setting this
property makes your UI respond when the user interacts with the uicontrol.
Callback property has no specified value,
then nothing happens when the end user interacts with the uicontrol.
This table lists the callback properties that are available, the user actions that trigger the callback function, and the most common UI and graphics components that use them.
Components That Use This Property
End user presses a mouse button while the pointer is on the component or figure.
End user triggers the component. For example: selecting a menu item, moving a slider, or pressing a push button.
End user edits a value in a table whose cells are editable.
End user selects cells in a table.
End user clicks the push tool or toggle tool with the left mouse button.
The figure closes.
Callback executes when MATLAB creates the object, but before it is displayed.
Callback executes just before MATLAB deletes the figure.
End user presses a keyboard key while the pointer is on the object.
End user releases a keyboard key while the pointer is on the object.
Executes when the
Executes when the
End user resizes a button group, figure, or panel whose
End user selects a different radio button or toggle button within a button group.
End user presses a mouse button while the pointer is in the figure window.
End user moves the pointer within the figure window.
End user releases a mouse button.
End user presses a key while the pointer is on the figure or any of its child objects.
End user releases a key while the pointer is on the figure or any of its child objects.
End user turns the mouse wheel while the pointer is on the figure.
After you add a
to your UI, but before you save it, GUIDE populates the
with the value,
%automatic. This value indicates
that GUIDE will generate a name for the callback function.
When you save your UI, GUIDE adds an empty callback function
definition to your UI code file, and it sets the control's
to be an anonymous function. This function definition is an example
of a GUIDE-generated callback function for a push button.
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) end
myui, then GUIDE sets the push button's
Callbackproperty to the following value:
pushbutton1_Callback. This anonymous function has four input arguments. The first argument is a string containing the name of the callback function. The last three arguments are provided by Handle Graphics®, and are discussed in the section, GUIDE Callback Syntax.
Note: GUIDE does not automatically generate callback functions for other UI components, such as tables, panels, or button groups. If you want any of these components to execute a callback function, then you must create the callback by right-clicking on the component in the layout, and selecting an item under View Callbacks in the context menu.
All callbacks must accept at least three input arguments:
hObject — Handle to the
UI component that triggered the callback.
eventdata — A variable that
contains detailed information about specific mouse or keyboard actions.
handles — A
contains handles to all the objects in the UI. GUIDE uses the
guidata function to store and maintain
For the callback function to accept additional arguments, you must put the additional arguments at the end of the argument list in the function definition.
eventdata argument provides detailed
information to certain callback functions. For example, if the end
user triggers the
KeyPressFcn, then MATLAB provides
information regarding the specific key (or combination of keys) that
the end user pressed. If
eventdata is not available
to the callback function, then MATLAB passes it as an empty array.
The following table lists the callbacks and components that use
GUIDE creates the name of a callback function by combining the
Tag property and the callback
property name. If you change the component's
then GUIDE changes the callback's name the next time you save the
If you decide to change the
Tag value after
saving the UI, then GUIDE updates the following items (assuming that
all components have unique
Component's callback function definition
Component's callback property value
References in the code file to the corresponding field
To rename a callback function without changing the component's
Change the name in the callback function definition.
Update the component's callback property by changing the first argument passed to the anonymous function. For example, the original callback property for a push button might look like this:
In this example, you must change the string,
the new function name.
Change all other references to the old function name to the new function name in the UI code file.
You can delete a callback function when you want to remove or change the function that executes when the end user performs a specific action. To delete a callback function:
Search and replace all instances that refer to the callback function in your code.
Open the UI in GUIDE and replace all instances that refer to the callback function in the Property Inspector.
Delete the callback function.