str2func - Construct function handle from character vector - MATLAB (original) (raw)
Main Content
Construct function handle from character vector
Syntax
Description
fh = str2func([str](#butxv26-c))
constructs a function handle, fh
, from a function name or text representation of an anonymous function.
Function handles created using str2func
do not have access to variables outside of their local workspace or to nested functions. If your function handle contains these variables or functions, MATLAB® throws an error when you invoke the handle. Also, if you use a text representation of an anonymous function, the resulting function handle does not have access to private or local functions.
Examples
Convert Character Vector to Function Handle
Convert the character vector 'ones'
to a function handle, and call the ones
function using the handle.
c = 'ones'; fh = str2func(c)
fh = functionhandle with value: @ones
Convert Character Vector to Handle to Anonymous Function
Convert a character vector that represents an anonymous function to a function handle. Workspace variables are not available to thestr2func
function. Therefore, include values in the character vector that are necessary to evaluate the expression and that are not defined as function inputs.
Define a character vector that represents the anonymous function 7_x_ – 13. Convert the character vector to a function handle.
str = '@(x)7*x-13'; fh = str2func(str)
Call the anonymous function using the handle.
If you include workspace variables in your character vector,str2func
creates the function handle, but MATLAB throws an error when you invoke the function handle.
a = 13; str = '@(x)7*x-a'; fh = str2func(str);
fh(3)
Undefined function or variable 'a'.
Error in @(x)7*x-a
Examine Differences Between str2func
and eval
Create a function that returns two function handles used to simulate the roll of dice. The first die (d1
) returns a number from 1 through 6, but the second die (d2
) always returns the number 1.
Create the following function in a folder on your MATLAB path. When str2func
is used with a character vector representing an anonymous function, it does not have access to the local function. Therefore, MATLAB calls the built-in randi
function, and returns a number from 1 through 6. The eval
function does have access to the local function, so d2
uses the overloaded randi
and always returns 1.
function [d1,d2] = diceRoll str = '@()randi([1 6],1)'; d1 = str2func(str); d2 = eval(str); end
function r = randi(,)
r = 1;
end
At the command prompt, call the diceRoll
function.
p1 =
function_handle with value:
@()randi([1,6],1)
p2 =
function_handle with value:
@()randi([1,6],1)
Both p1
and p2
appear to be associated with the same anonymous function.
Invoke the function handles. The result from p1
varies from 1 through 6. The result from p2
is always 1.
Input Arguments
str
— Text to convert to function handle
function name | character vector representation of anonymous function | string scalar representation of anonymous function
Text to convert to a function handle, specified as a function name or a character vector or string scalar representation of an anonymous function.
Example: str = 'cos'
Example: str = '@(x) x.^2'
Tips
- A function handle that stores variable values does not retain its original value when you use
func2str
to convert it to a character vector, and then convert it back to a handle withstr2func
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- The input argument must be constant/known at compile time.
- Code generation does not support an input argument that represents an anonymous function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a