Also, if
you remove the path to repmat.m from the search path, MATLAB is still able
to locate and evaluate the function using the handle that you created prior to
the path change.
21-5
21 Function Handles
Since repmat is not an overloaded function in this case, evaluation of the
function through its handle is fairly simple. You call feval on the function
handle, also passing any arguments the function should act upon. MATLAB
executes the one function whose access information is stored in the handle.
21-6
Constructing a Function Handle
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.
Maximum Length of a Function Name
Function names used in handles are unique up to 31 characters. If the function
name exceeds that length, MATLAB truncates the latter part of the name.
fhandle = @function_name_that_exceeds_thirty_one_characters
fhandle =
@function_name_that_exceeds_thir
21-7
21 Function Handles
For function handles created for Java constructors, the length of any segment
of the package name or class name must not exceed 31 characters. (The term
segment refers to any portion of the name that lies before, between, or after a
dot. For example, there are three segments in java.lang.String.) There is no
limit to the overall length of the string specifying the package and class.
The following statement is valid, even though the length of the overall package
and class specifier exceeds 31 characters.
fhandle = @java.awt.datatransfer.StringSelection
21-8
Evaluating a Function Through Its Handle
Evaluating a Function Through Its Handle
Execute the target function of a function handle using the MATLAB feval
command. The syntax for using this command with a function handle is
feval(fhandle, arg1, arg2, ..., argn)
This acts similarly to a direct call to the function represented by fhandle,
passing arguments arg1 through argn. The principal differences are:
• A function handle can be evaluated from within any function that you pass
it to.
• The code source that MATLAB selects for evaluation depends upon which
overloaded methods of the function were on the MATLAB path and in scope
at the time the handle was constructed. (Argument types also affect method
selection.) Path and scope are not considered at the time of evaluation.
• MATLAB does the work of initial function lookup at the time the function
handle is constructed. This does not need to be done each time MATLAB
evaluates the handle.
Note The feval command does not operate on nonscalar function handles.
Passing a nonscalar function handle to feval results in an error.
Function Evaluation and Overloading
To understand the relationship between function handles and overloading, it is
helpful to review, briefly, the nature of MATLAB function calls. Because of
overloading, it is useful to think of a single MATLAB function as comprising a
number of code sources (for example, built-in code, M-files). When you call a
MATLAB function without feval, the choice of which source is called depends
upon two factors:
• The methods that are visible on the path at the time of the call
• The classes of the arguments to the function
MATLAB evaluates function handles in a similar manner. In most cases, a
function handle represents a collection of methods that overload the function.
21-9
21 Function Handles
When you evaluate a function handle using feval, the choice of the particular
method called depends on:
• The methods that were visible on the path at the time the handle was
constructed
• The classes of the arguments passed with the handle to the feval command
Examples of Function Handle Evaluation
This section provides two examples of how function handles are used and
evaluated.
Example 1 - A Simple Function Handle
The following example defines a function, called plot_fhandle, that receives a
function handle and data, and then performs an evaluation of the function
handle on that data.
function x = plot_fhandle(fhandle, data)
plot(data, feval(fhandle, data))
When you call plot_fhandle with a handle to the sin function and the
argument shown below, the resulting evaluation produces the following plot.
plot_fhandle(@sin, -pi:0.01:pi)
21-10
Evaluating a Function Through Its Handle
Example 2 - Function Handles and Subfunctions
The M-file in this example defines a primary function, fitcurvedemo, and a
subfunction called expfun. The subfunction, by definition, is visible only within
the scope of its own M-file. This, of course, means that it is available for use
only by other functions within that M-file.
The author of this code would like to use expfun outside the confines of this one
M-file. This example creates a function handle to the expfun subfunction,
storing access information for the subfunction so that it can be called from
anywhere in the MATLAB environment. The function handle is passed to