Construct function handle from function name string




str2func('str') constructs a function handle fhandle for the function named in the string 'str'. The contents of str can be the name of a file that defines a MATLAB® function, or the name of an anonymous function.

You can create a function handle fh using any of the following four methods:

  • Create a handle to a named function:

    fh = @functionName;
    fh = str2func('functionName');
  • Create a handle to an anonymous function:

    fh = @(x)functionDef(x);
    fh = str2func('@(x)functionDef(x)');

You can create an array of function handles from strings by creating the handles individually with str2func, and then storing these handles in a cell array.


Example 1

To convert the string, 'sin', into a handle for that function, type

fh = str2func('sin')
fh = 

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:

function fh = makeHandle(funcname)
fh = str2func(funcname);

This is the code that calls makeHandle to construct the function handle:

ans = 

Example 3

To call str2func on a cell array of strings, use the cellfun function. This returns a cell array of function handles:

fh_array = cellfun(@str2func, {'sin' 'cos' 'tan'}, ...
                   'UniformOutput', false);

ans =

Example 4

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);
fminbnd(fhandle, lower, upper)

Whether you call myminbnd with a function handle or function name string, the function can handle the argument appropriately:

myminbnd('humps', 0.3, 1)
converting function string to function handle ...
ans =

Example 5

The dirByType function shown here creates an anonymous function called dirCheck. What the anonymous function does depends upon the value of the dirType argument passed in to the primary function. The example demonstrates one possible use of str2func with anonymous functions:

function dirByType(dirType)
   case 'class', leadchar = '@';
   case 'package', leadchar = '+';
   otherwise disp('ERROR: Unrecognized type'), return;

dirfile = @(fs)isdir(;
dirCheckStr = ['@(fs)strcmp(,1),''', leadchar, ''')'];
dirCheckFun = str2func(dirCheckStr);
s = dir;   filecount = length(s);

for k=1:filecount
   fstruct = s(k);
   if dirfile(fstruct) && dirCheckFun(fstruct)
       fprintf('%s folder: %s\n', dirType,

Generate a list of class and package folders:

class folder: @Point
class folder: @asset
class folder: @bond

package folder: +containers
package folder: +event
package folder: +mypkg

More About

collapse all


Nested functions are not accessible to str2func. To construct a function handle for a nested function, you must use the function handle constructor, @.

Any variables and their values originally stored in a function handle when it was created are lost if you convert the function handle to a string and back again using the func2str and str2func functions.

Introduced before R2006a

Was this topic helpful?