Graphics    

Function Handle Callbacks

Handle Graphics objects have a number of properties for which you can define callback functions. When a specific event occurs (e.g., a user clicks on a push button or deletes a figure), the corresponding callback function executes. You can specify the value of a callback property as a

This section illustrates how to define function handle callbacks for Handle Graphics objects. For information on function handles and how to use them, see the function handle reference page.

Function Handle Syntax

In Handle Graphics, functions that you want to use as function handle callbacks must define at least two input arguments in the function definition:

MATLAB passes these two arguments implicitly whenever the callback executes. For example, consider the following statements that are contained in a single M-file.

The first statement creates a figure and assigns a function handle to its WindowButtondownFcn property (created by using the @ symbol before the function name). This function handle points to the subfunction myCallback. The definition of myCallback must specify the two required input arguments in its function definition line.

Passing Additional Input Arguments

You can define the callback function to accept additional input arguments by adding them to the function definition. For example,

When using additional arguments for the callback function, you must set the value of the property to a cell array (i.e., enclose the function handle and arguments in curly braces). For example,

Defining Callbacks as a Cell Array of Strings -- Special Case

Defining a callback as a cell array of strings is a special case because MATLAB treats it differently from a simple string. Setting a callback property to a string causes MATLAB to evaluate that string in the base workspace when the callback is invoked. However, setting a callback to a cell array of strings behaves as follows:

For example,

requires you to define a function M-file that uses three arguments,


  Callback Properties for Graphics Objects Why Use Function Handle Callbacks