| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
str2func('str')
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 function M-file, 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.
Nested functions are not accessible to str2func. To construct a function handle for a nested function, you must use the function handle constructor, @.
To convert the string, 'sin', into a handle for that function, type
fh = str2func('sin')
fh =
@sinIf 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:
function fh = makeHandle(funcname) fh = str2func(funcname);
This is the code that calls makdHandle to construct the function handle:
makeHandle('sin')
ans =
@sinTo 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.2837In 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.6370The 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 directory: %s\n', dirType, fstruct.name)
end
end
Generate a list of class and package directories:
dirByType('class')
class directory: @Point
class directory: @asset
class directory: @bond
dirByType('package')
package directory: +containers
package directory: +event
package directory: +mypkgfunction_handle, func2str, functions
![]() | str2double | str2mat | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |