Construct function handle from string




fh = str2func(str) constructs a function handle, fh, from a function name or a string that defines 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 an anonymous function string, the resulting function handle does not have access to private or local functions.


collapse all

Convert String to Function Handle

Convert the string 'ones' to a function handle, and call the ones function using the handle.

str = 'ones';
fh = str2func(str)

fh = 


ans =

     1     1     1     1     1

Convert String to Handle to Anonymous Function

Convert a string that represents an anonymous function to a function handle. Workspace variables are not available to the str2func function. Therefore, include values in the string that are necessary to evaluate the expression and that are not defined as function inputs.

Define a string that represents the anonymous function 7x – 13. Convert the string to a function handle.

str = '@(x)7*x-13';
fh = str2func(str)
fh = 


Call the anonymous function using the handle.

ans =


If you include workspace variables in your string, 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);

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 an anonymous function string, 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.

% Copyright 2015 The MathWorks, Inc.

function [d1,d2] = diceRoll
str = '@()randi([1 6],1)';
d1 = str2func(str);
d2 = eval(str);

function r = randi(~,~)
r = 1;

At the command prompt, call the diceRoll function.

[p1,p2] = diceRoll
p1 = 


p2 = 


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.

ans =


ans =


Input Arguments

collapse all

str — String to convert to function handlefunction name | anonymous function string

String to convert to a function handle, specified as a function name or anonymous function string.

Example: str = 'cos'

Example: str = '@(x) x.^2'

More About

expand all


  • A function handle that stores variable values does not retain its original value when you use func2str to convert it to a string, and then convert it back to a handle with str2func.

See Also


Introduced before R2006a

Was this topic helpful?