Graphics Previous page   Next 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, which 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 requires the following:

For example,

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


Previous page  Function Handle Callbacks Why Use Function Handle Callbacks Next page

© 1994-2005 The MathWorks, Inc.