Next: , Up: Function Handles Inline Functions and Anonymous Functions


11.10.1 Function Handles

A function handle is a pointer to another function and is defined with the syntax

     @function-name

For example,

     f = @sin;

creates a function handle called f that refers to the function sin.

Function handles are used to call other functions indirectly, or to pass a function as an argument to another function like quad or fsolve. For example:

     f = @sin;
     quad (f, 0, pi)
         ⇒ 2

You may use feval to call a function using function handle, or simply write the name of the function handle followed by an argument list. If there are no arguments, you must use an empty argument list ‘()’. For example:

     f = @sin;
     feval (f, pi/4)
         ⇒ 0.70711
     f (pi/4)
         ⇒ 0.70711

— Built-in Function: functions (fcn_handle)

Return a struct containing information about the function handle fcn_handle.

— Built-in Function: func2str (fcn_handle)

Return a string containing the name of the function referenced by the function handle fcn_handle.

— Built-in Function: str2func (fcn_name)
— Built-in Function: str2func (fcn_name, "global")

Return a function handle constructed from the string fcn_name. If the optional "global" argument is passed, locally visible functions are ignored in the lookup.