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 character vector containing a MATLAB® expression.
Setting this property makes your app respond when the user interacts
with the uicontrol. If the Callback
property
has no specified value, then nothing happens when the 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.
Callback Property | User Action | Components That Use This Property |
---|---|---|
ButtonDownFcn | End user presses a mouse button while the pointer is on the component or figure. | |
Callback | End user triggers the component. For example: selecting a menu item, moving a slider, or pressing a push button. | |
CellEditCallback | End user edits a value in a table whose cells are editable. | |
CellSelectionCallback | End user selects cells in a table. | |
ClickedCallback | End user clicks the push tool or toggle tool with the left mouse button. | |
CloseRequestFcn | The figure closes. | |
CreateFcn | Callback executes when MATLAB creates the object, but before it is displayed. |
|
DeleteFcn | Callback executes just before MATLAB deletes the figure. |
|
KeyPressFcn | 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. | |
OffCallback | Executes when the | |
OnCallback | Executes when the | |
| End user resizes a button group, figure, or panel whose | |
SelectionChangedFcn | End user selects a different radio button or toggle button within a button group. | |
WindowButtonDownFcn | End user presses a mouse button while the pointer is in the figure window. | |
WindowButtonMotionFcn | End user moves the pointer within the figure window. | |
WindowButtonUpFcn | End user releases a mouse button. | |
WindowKeyPressFcn | End user presses a key while the pointer is on the figure or any of its child objects. | |
WindowKeyReleaseFcn | 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. |
To associate a callback function with a UI component, set the value of one of the component’s callback properties to be a reference to the callback function. Typically, you do this when you define the component, but you can change callback property values anywhere in your code.
Specify the callback property value in one of the following ways:
Specify a Cell Array. This cell array contains a function handle as the first element, followed by and any input arguments you want to use in the function.
Specify a Character Vector Containing MATLAB Commands (Not Recommended)
Function handles provide a way to represent a function as a variable. The function must be a local or nested function in the same file as the app code, or you can write it in a separate file that is on the MATLAB path.
To create the function handle, specify the @
operator before
the name of the function. For example, this uicontrol
command
specifies the Callback
property to be a handle to the function
pushbutton_callback
:
b = uicontrol('Style','pushbutton','Callback',@pushbutton_callback);
Here is the function definition for
pushbutton_callback
:
function pushbutton_callback(src,event) display('Button pressed'); end
Callback Property Name | Component |
---|---|
WindowKeyPressFcn WindowKeyReleaseFcn WindowScrollWheel | figure |
KeyPressFcn | figure , uicontrol , uitable |
KeyReleaseFcn | figure , uicontrol , uitable |
SelectionChangedFcn | uibuttongroup |
CellEditCallback CellSelectionCallback | uitable |
A benefit of specifying callbacks as function handles is that MATLAB checks the function for syntax errors and missing dependencies when you assign the callback to the component. If there is a problem in the callback function, then MATLAB returns an error immediately instead of waiting for the user to trigger the callback. This behavior helps you to find problems in your code before the user encounters them.
Use a cell array to specify a callback function that accepts additional input arguments that you want to use in the function. The first element in the cell array is a function handle. The other elements in the cell array are the additional input arguments you want to use, separated by commas. The function you specify must define the same two input arguments as described in Specify a Function Handle. However, you can define additional inputs in your function declaration after the first two arguments.
This uicontrol
command creates a push button and specifies
the Callback
property to be a cell array. In this case, the name
of the function is pushbutton_callback
, and the value of the
additional input argument is
5
.
b = uicontrol('Style','pushbutton','Callback',{@pushbutton_callback,5});
Here is the function definition for
pushbutton_callback
:
function pushbutton_callback(src,event,x) display(x); end
Like callbacks specified as function handles, MATLAB checks callbacks specified as cell arrays for syntax errors and missing dependencies when you assign the callback to the component. If there is a problem in the callback function, then MATLAB returns an error immediately instead of waiting for the user to trigger the callback. This behavior helps you to find problems in your code before the user encounters them.
Specify an anonymous function when you want a UI component to execute a function
that does not support the two arguments that are required for function handles and
cell arrays. For example, this uicontrol
command creates a push
button and specifies the Callback
property to be an anonymous
function. In this case, the name of function is myfun
, and its
function declaration defines only one input argument, x
.
b = uicontrol('Style','pushbutton','Callback',@(src,event)myfun(x));
You can specify a character vector when you want to execute a few simple commands, but the callback can become difficult to manage if it contains more than a few commands. The character vector you specify must consist of valid MATLAB expressions, which can include arguments to functions. For example:
hb = uicontrol('Style','pushbutton',... 'String','Plot line',... 'Callback','plot(rand(20,3))');
'plot(rand(20,3))'
, is a valid command, and
MATLAB evaluates it when the user clicks the button. If the character vector
includes a variable, for
example,'plot(x)'
x
must exist in the base workspace when the user triggers
the callback, or it returns an error. The variable does not need to exist at the time
you assign callback property value, but it must exist when the user triggers the
callback.Unlike callbacks that are specified as function handles or cell arrays, MATLAB does not check character vectors for syntax errors or missing dependencies. If there is a problem with the MATLAB expression, it remains undetected until the user triggers the callback.