Programming Previous page   Next Page

Function Handles

A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks).

Constructing a Function Handle

Use the following syntax to construct a function handle, preceding the name of the function with an @ sign. Use only the function name, with no path information, after the @ sign:

MATLAB maps the handle to the function you specify and saves this mapping information in the handle. If there is more than one function with this name, MATLAB maps to the one function source it would dispatch to if you were actually calling the function.

A function handle retains that same mapping even if its corresponding function goes out of scope. For example, if, after creating the handle, you change the MATLAB path so that a different function of the same name now takes precedence, invoking the function handle still executes the code to which the handle was originally mapped.

Handles to Anonymous Functions

Another way to construct a function handle is to create an anonymous function. For example,

creates an anonymous function that computes the square of its input argument x. The variable sqr contains a handle to the anonymous function. See Anonymous Functions for more information.

Arrays of Function Handles

To store function handles in an array, use a cell array:

Invalid or Obsolete Function Handles

If you create a handle to a function that is not on the MATLAB path, or if you load a handle to a function that is no longer on the path, MATLAB catches the error only when the handle is invoked. You can assign an invalid handle and use it in such operations as func2str. MATLAB catches and reports an error only when you attempt to use it in a runtime operation.

Calling a Function Using Its Handle

To execute a function associated with a function handle, use the syntax shown here, treating the function handle fhandle as if it were a function name:

If the function being called takes no input arguments, then use empty parentheses after the function handle name:

Simple Function Handle Example

The following example calls a function plotFHandle, passing it a handle for the MATLAB sin function. plotFHandle then calls the plot function, passing it some data and the function handle to sin. The plot function calls the function associated with the handle to compute its y-axis values:

Call plotFhandle with a handle to the sin function and the value shown below:

Functions That Operate on Function Handles

MATLAB provides the following functions for working with function handles. See the reference pages for these functions for more information.

Return information describing a function handle.
Construct a function name string from a function handle.
Construct a function handle from a function name string.
Save a function handle from the current workspace to a MAT-file.
Load a function handle from a MAT-file into the current workspace.
Determine if a variable contains a function handle.
Determine if two function handles are handles to the same function.

Previous page  Returning Output Arguments Additional Information on Function Handles Next page

© 1994-2005 The MathWorks, Inc.