Syntax
Description
eval(expression)
executes expression
, a string containing any valid MATLAB expression. You can construct expression
by concatenating substrings and variables inside square brackets:
eval(expression,catch_expr)
executes expression
and, if an error is detected, executes the catch_expr
string. If expression
produces an error, the error string can be obtained with the lasterr
function. This syntax is useful when expression
is a string that must be constructed from substrings. If this is not the case, use the try...catch
control flow statement in your code.
[a1,a2,a3,...] = eval(function(b1,b2,b3,...))
executes function
with arguments b1,b2,b3,...
, and returns the results in the specified output variables.
Remarks
Using the eval
output argument list is recommended over including the output arguments in the expression string. The first syntax below avoids strict checking by the MATLAB parser and can produce untrapped errors and other unexpected behavior.
eval('[a1,a2,a3,...] = function(var)') % not recommended
[a1,a2,a3,...] = eval('function(var)') % recommended syntax
Examples
This for
loop generates a sequence of 12 matrices named M1
through M12
:
This example uses a function showdemo
that runs a MATLAB demo selected by the user. If an error is encountered, a message is displayed that names the demo that failed.
function showdemo(demos)
errstring = 'Error running demo: ';
n = input('Select a demo number: ');
eval(demos(n,:),'[errstring demos(n,:)]')
% ----- end of file showdemo.m -----
D = ['odedemo'; 'quademo'; 'fitdemo'];
showdemo(D)
Select a demo number: 2
ans =
Error running demo: quademo
The next example executes the size function on a 3-dimensional array, returning the array dimensions in output variables d1
, d2
, and d3
.
evalc
Evaluate MATLAB expression with capture
Syntax
Description
T = evalc(S)
is the same as eval
(S
) except that anything that would normally be written to the command window is captured and returned in the character array T
(lines in T
are separated by \n
characters).
T = evalc(s1,s2)
is the same as eval(s1,s2)
except that any output is captured into T
.
[T,X,Y,Z,...] = evalc(S)
is the same as [X,Y,Z,...] = eval(S)
except that any output is captured into T
.
Remark
When you are using evalc
, diary
, more
, and input
are disabled.
evalin
Execute a string containing a MATLAB expression in a workspace
Syntax
Description
evalin(ws,
executes expression
) expression
, a string containing any valid MATLAB expression, in the context of the workspace ws
. ws
can have a value of 'base
' or 'caller
' to denote the MATLAB base workspace or the workspace of the caller function. You can construct expression
by concatenating substrings and variables inside square brackets:
[a1,a2,a3,...] = evalin(ws,
executes expression
) expression
and returns the results in the specified output variables. Using the evalin
output argument list is recommended over including the output arguments in the expression string:
The above syntax avoids strict checking by the MATLAB parser and can produce untrapped errors and other unexpected behavior.
evalin(ws,
executes expression
,
catch_expr
) expression
and, if an error is detected, executes the catch_expr
string. If expression
produces an error, the error string can be obtained with the lasterr
function. This syntax is useful when expression
is a string that must be constructed from substrings. If this is not the case, use the try...catch
control flow statement in your code.
Remarks
The MATLAB base workspace is the workspace that is seen from the MATLAB command line (when not in the debugger). The caller workspace is the workspace of the function that called the M-file. Note, the base and caller workspaces are equivalent in the context of an M-file that is invoked from the MATLAB command line.
Examples
This example extracts the value of the variable var
in the MATLAB base workspace and captures the value in the local variable v
:
Limitation
evalin
cannot be used recursively to evaluate an expression. For example, a sequence of the form evalin('caller','evalin(''caller'',''x'')')
doesn't work.
No comments:
Post a Comment