This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Callback Definition

Ways to Specify Callbacks

To use callback properties, assign the callback code to the property. Use one of the following techniques:

  • A function handle that references the function to execute.

  • A cell array containing a function handle and additional arguments

  • A character vector that evaluates to a valid MATLAB® expression. MATLAB evaluates the character vector in the base workspace.

Defining a callback as a character vector is not recommended. The use of a function specified as function handle enables MATLAB to provide important information to your callback function.

For more information, see Callback Function Syntax.

Callback Function Syntax

Graphics callback functions must accept at least two input arguments:

  • The handle of the object whose callback is executing. Use this handle within your callback function to refer to the callback object.

  • The event data structure, which can be empty for some callbacks or contain specific information that is described in the property description for that object.

Whenever the callback executes as a result of the specific triggering action, MATLAB calls the callback function and passes these two arguments to the function .

For example, define a callback function called lineCallback for the lines created by the plot function. With the lineCallback function on the MATLAB path, use the @ operator to assign the function handle to the ButtonDownFcn property of each line created by plot.


Define the callback to accept two input arguments. Use the first argument to refer to the specific line whose callback is executing. Use this argument to set the line Color property:

function lineCallback(src,evt)
   src.Color = 'red';

The second argument is empty for the ButtonDownFcn callback. The ~ character indicates that this argument is not used.

Passing Additional Input Arguments

To define additional input arguments for the callback function, add the arguments to the function definition, maintaining the correct order of the default arguments and the additional arguments:

function lineCallback(src,evt,arg1,arg2)
   src.Color = 'red';
   src.LineStyle = arg1;
   src.Marker = arg2;

Assign a cell array containing the function handle and the additional arguments to the property:


You can use an anonymous function to pass additional arguments. For example:


Related Information

For information on using anonymous functions, see Anonymous Functions.

For information about using class methods as callbacks, see Class Methods for Graphics Callbacks.

For information on how MATLAB resolves multiple callback execution, see the BusyAction and Interruptible properties of the objects defining callbacks.

Define a Callback as a Default

You can assign a callback to the property of a specific object or you can define a default callback for all objects of that type.

To define a ButtonDownFcn for all line objects, set a default value on the root level.

  • Use the groot function to specify the root level of the object hierarchy.

  • Define a callback function that is on the MATLAB path

  • Assign a function handle referencing this function to the defaultLineButtonDownFcn.


The default value remains assigned for the MATLAB session. You can make the default value assignment in your startup.m file.

Was this topic helpful?