Programming |
Function Handles
A function handle is a MATLAB value and data type 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).
Read more about function handles in the section, Function Handles.
Constructing and Invoking a Function Handle
You construct a handle for a specific function by preceding the function name with an @
sign. Use only the function name (with no path information) after the @
sign:
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.
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:
Function Summary | MATLAB Classes |
© 1994-2005 The MathWorks, Inc.