evalc
Evaluate MATLAB expression and capture results
Description
evaluates the MATLAB® code represented by results
= evalc(expression
)expression
and captures
anything that would normally be written to the Command Window in
results
.
Note
Security Considerations: When calling
evalc
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
evalc
. First, search for allowed operations. Then, if you find other operations, disallow execution.Replace
evalc
with an alternative. For more information, see Alternatives to the eval Function.
Performance Considerations: In most cases, using the
evalc
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
evalc
.
[
additionally returns the outputs from results
,output1,...,outputN
] = evalc(expression
)expression
in the
specified variables.
Examples
Input Arguments
Output Arguments
Limitations
When using
evalc
, the functionsdiary
,more
, andinput
are disabled.If you use
evalc
within an anonymous function, nested function, or function that contains a nested function, the evaluatedexpression
does not create any variables.
Tips
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
evalc
function. For example, the statementresult = evalc(['output = ',expression])
is not recommended.Instead, specify output arguments to the
evalc
function to store the results of the evaluated expression. For example:[result,output] = evalc(expression)
Extended Capabilities
Version History
Introduced before R2006a