Evaluate MATLAB expression and capture results
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.
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 to evaluate the expression
magic(5) and store the results.
results = evalc('magic(5)')
results = ' ans = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 '
expression— Expression to evaluate
Expression to evaluate, specified as a character vector or string scalar.
expression must be a valid MATLAB expression and must not include any MATLAB keywords. To determine whether a word is a MATLAB keyword, use the
results— Captured Command Window output
Captured Command Window output, returned as a character array. Individual
lines in the captured output are separated by
output1,...,outputN— Outputs from evaluated expression
Outputs from evaluated expression, returned as any MATLAB data type.
evalc, the functions
input are disabled.
If you use
evalc 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
evalc function. For example, the statement
result = evalc(['output = ',expression]) is not
Instead, specify output arguments to the
to store the results of the evaluated expression. For example:
[result,output] = evalc(expression)
backgroundPoolor accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.