Documentation Center

  • Trials
  • Product Updates

str2func

Construct function handle from function name string

Syntax

str2func('str')

Description

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.

Examples

Example 1

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

fh = str2func('sin')
fh = 
    @sin

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:

makeHandle('sin')
ans = 
    @sin

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);

fh_array{2}(5)
ans =
   0.2837

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);
end
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 =
    0.6370

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)
switch(dirType)
   case 'class', leadchar = '@';
   case 'package', leadchar = '+';
   otherwise disp('ERROR: Unrecognized type'), return;
end

dirfile = @(fs)isdir(fs.name);
dirCheckStr = ['@(fs)strcmp(fs.name(1,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, fstruct.name)
   end
end

Generate a list of class and package folders:

dirByType('class')
class folder: @Point
class folder: @asset
class folder: @bond

dirByType('package')
package folder: +containers
package folder: +event
package folder: +mypkg

More About

expand all

Tips

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.

See Also

| |

Was this topic helpful?