|MATLAB Function Reference|
MATLAB data type that is a handle to a function
handle = @functionname
returns a handle to the specified MATLAB function.
A function handle captures all the information about a function that MATLAB needs to execute that function. Typically, a function handle is passed in an argument list to other functions. The receiving functions can then execute the function through the handle that was passed in. Always use
feval to execute, or evaluate, a function through its function handle.
When creating a function handle, the function you specify must be on the MATLAB path and in the current scope. This condition does not apply when you evaluate the function handle. You can, for example, execute a subfunction from a separate (out of scope) M-file using a function handle, as long as the handle was created within the subfunction's M-file (in scope).
For nonoverloaded functions, subfunctions, and private functions, a function handle references just the one function specified in the
When you evaluate an overloaded function through its handle, the arguments the handle is evaluated with determine the actual function that MATLAB dispatches to.
The function handle is a standard MATLAB data type. As such, you can manipulate and operate on function handles in the same manner as on other MATLAB data types. This includes using function handles in arrays, structures, and cell arrays.
Function handles enable you to do all of the following:
The following example creates a function handle for the
humps function and assigns it to the variable,
Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to
fminbnd, which then minimizes over the interval [0.3, 1].
fminbnd function evaluates the
@humps function handle using
feval. A small portion of the
fminbnd M-file is shown below. In line 1, the
funfcn input parameter receives the function handle,
@humps, that was passed in. The
feval statement, in line 113, evaluates the handle.