Programming and Data Types | ![]() ![]() |
Constructing a Function Handle
Construct a function handle in MATLAB using the at sign, @
, before the function name. The following example creates a function handle for the humps
function and assigns it to the variable fhandle
.
fhandle = @humps;
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]
.
x = fminbnd(fhandle, 0.3, 1) x = 0.6370
The 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.
1 function [xf,fval,exitflag,output] = ... fminbnd(funfcn,ax,bx,options,varargin).
.
.
113 fx = feval(funfcn,x,varargin{:});
Note
When creating a function handle, you may only use the function name
after the @ sign. This must not include any path information. The following
syntax is invalid: fhandle = @\home\user4\humps .
|
![]() | A Simple Function Handle | Maximum Length of a Function Name | ![]() |