Create Function Handle - MATLAB & Simulink (original) (raw)

What Is a Function Handle?

A function handle is a MATLAB® data type that stores an association to a function. Indirectly calling a function enables you to invoke the function regardless of where you call it from. Typical uses of function handles include:

You can see if a variable, h, is a function handle using isa (h,'function_handle').

Creating Function Handles

To create a handle for a function, precede the function name with an @ sign. For example, if you have a function called myfunction, create a handle named f as follows:

You call a function using a handle the same way you call the function directly. For example, suppose that you have a function named computeSquare, defined as:

function y = computeSquare(x) y = x.^2; end

Create a handle and call the function to compute the square of four.

f = @computeSquare; a = 4; b = f(a)

If the function does not require any inputs, then you can call the function with empty parentheses, such as

Without the parentheses, the assignment creates another function handle.

Function handles are variables that you can pass to other functions. For example, calculate the integral of _x_2 on the range [0,1].

Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location. You do not have to specify the path to the function when creating the handle, only the function name.

Keep the following in mind when creating handles to functions:

Anonymous Functions

You can create handles to anonymous functions. An anonymous function is a one-line expression-based MATLAB function that does not require a program file. Construct a handle to an anonymous function by defining the body of the function, anonymous_function, and a comma-separated list of input arguments to the anonymous function, arglist. The syntax is:

h = @(arglist)anonymousfunction

For example, create a handle, sqr, to an anonymous function that computes the square of a number, and call the anonymous function using its handle.

sqr = @(n) n.^2; x = sqr(3)

For more information, see Anonymous Functions.

Arrays of Function Handles

You can create an array of function handles by collecting them into a cell or structure array. For example, use a cell array:

C = {@sin, @cos, @tan}; C{2}(pi)

Or use a structure array:

S.a = @sin; S.b = @cos; S.c = @tan; S.a(pi/2)

Saving and Loading Function Handles

You can save and load function handles in MATLAB, as you would any other variable. In other words, use thesave and load functions. If you save a function handle, MATLAB saves the absolute path information. You can invoke the function from any location that MATLAB is able to reach, as long as the file for the function still exists at this location. An invalid handle occurs if the file location or file name has changed since you created the handle. If a handle is invalid, MATLAB might display a warning when you load the file. When you invoke an invalid handle, MATLAB issues an error.

See Also

str2func | func2str | functions | isa | function_handle

More About