Main Content

Create Function Handle

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:

  • Passing a function to another function (often called function functions). For example, passing a function to integration and optimization functions, such as integral and fzero.

  • Specifying callback functions (for example, a callback that responds to a UI event or interacts with data acquisition hardware).

  • Constructing handles to functions defined inline instead of stored in a program file (anonymous functions).

  • Calling local functions from outside the main function.

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:

f = @myfunction;

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)
b =

    16

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

h = @ones;
a = h()
a =

    1

Without the parentheses, the assignment creates another function handle.

a = h
a = 

    @ones

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

q = integral(f,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:

  • Name length — Each part of the function name (including package and class names) must be less than the number specified by namelengthmax. Otherwise, MATLAB truncates the latter part of the name.

  • Scope — The function must be in scope at the time you create the handle. Therefore, the function must be on the MATLAB path or in the current folder. Or, for handles to local or nested functions, the function must be in the current file.

  • Precedence — When there are multiple functions with the same name, MATLAB uses the same precedence rules to define function handles as it does to call functions. For more information, see Function Precedence Order.

  • Overloading — When a function handle is invoked with one or more arguments, MATLAB determines the dominant argument. If the dominant argument is an object, MATLAB determines if the object’s class has a method which overloads the same name as the function handle’s associated function. If it does, then the object’s method is invoked instead of the associated function.

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)anonymous_function

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)
x =

     9

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)
ans =

    -1

Or use a structure array:

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

     1

Saving and Loading Function Handles

You can save and load function handles in MATLAB, as you would any other variable. In other words, use the save 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

| | | |

Related Examples

More About