- 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” on page 20-43. - Overloading — If the function you specify overloads a function in a class that is not a
fundamental MATLAB class, the function is not associated with the function handle at
the time it is constructed. Instead, MATLAB considers the input arguments and
determines which implementation to call at the time of evaluation.
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” on page 20-24.
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:
13 Function Handles