str2func
- :
hfcn =str2func(str)¶ Turn the text in str into a function handle.
str may be a bare function name, such as
"sin", in which case a handle to that named function is returned. It may instead be the full text of an anonymous function beginning with ‘@’, such as"@(x) sin (x + pi)"; that text is parsed and evaluated in the calling scope, so names it references resolve exactly as they would had the anonymous function been written there directly. An empty name is rejected as invalid. A second argument is accepted for compatibility but has no effect; supplying one produces a warning that it was ignored.Programming Note: when the function is known as you write the code, the plain handle syntax is clearer and lets the parser build the handle directly, so it is usually preferable to
str2func. For example:hfcn = @sin ; hfcn = @(x) sin (x + pi) ;