Evaluate MATLAB expression in specified workspace
evalin(
evaluates the MATLAB® code represented by workspace
,expression
)expression
using the
variables in the specified workspace.
Note
Security Considerations: When calling
evalin
with untrusted user input, validate the input to avoid
unexpected code execution. Examples of untrusted user input are data coming from a user you
might not know or from a source you have no control over. If you need to address this
concern, consider these approaches:
Validate inputs to evalin
. First, search for allowed
operations. Then, if you find other operations, disallow execution.
Replace evalin
with an alternative. For more
information, see Alternatives to the eval Function.
Performance Considerations: In most cases, using the
evalin
function is also less efficient than using other
MATLAB functions and language constructs, and the resulting code can be more
difficult to read and debug. Consider using an alternative to
evalin
.
[
returns the outputs from output1,...,outputN
] = evalin(workspace
,expression
)expression
in the specified
variables.
evalin('caller',expression)
finds only
variables in the caller's workspace; it does not find
functions in the caller. For this reason, you cannot
use evalin
to construct a handle to a function that is
defined in the caller.
evalin
cannot be used recursively to evaluate an
expression. For example, statement
evalin('caller','evalin(''caller'',''x'')')
is
invalid.
If you use evalin
within an anonymous function, nested
function, or function that contains a nested function, the evaluated
expression
does not create any variables.
To allow the MATLAB parser to perform stricter checks on your code and avoid untrapped
errors and other unexpected behaviors, do not include output arguments in the
input to the evalin
function. For example, the statement
evalin('base',['output = ',expression])
is not
recommended.
Instead, specify output arguments to the evalin
function
to store the results of the evaluated expression. For example:
output = evalin('base',expression)