MATLAB Function Reference |
Construct function handle from function name string
Syntax
Description
str2func('str')
constructs a function handle fhandle
for the function named in the string 'str'
.
You can create a function handle using either the @function
syntax or the str2func
command. You can also perform this operation on a cell array of strings. In this case, an array of function handles is returned.
Example 1
To convert the string, 'sin'
, into a handle for that function, type
Example 2
If you pass a function name string in a variable, the function that receives the variable can convert the function name to a function handle using str2func
. The example below passes the variable, funcname
, to function makeHandle
, which then creates a function handle. Here is the function M-file:
This is the code that calls makdHandle
to construct the function handle:
Example 3
In the following example, the myminbnd
function expects to receive either a function handle or string in the first argument. If you pass a string, myminbnd
constructs a function handle from it using str2func
, and then uses that handle in a call to fminbnd
:
function myminbnd(fhandle, lower, upper) if ischar(fhandle) disp 'converting function string to function handle ...' fhandle = str2func(fhandle); end fminbnd(fhandle, lower, upper)
Whether you call myminbnd
with a function handle or function name string, the function can handle the argument appropriately:
See Also
function_handle
, func2str
, functions
str2double | str2mat |
© 1994-2005 The MathWorks, Inc.