Evaluate MATLAB expression
eval( evaluates the
MATLAB® code in
Security Considerations: When calling
eval 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
eval. First, search for allowed operations. Then, if you find other operations, disallow execution.
evalwith an alternative. For more information, see Alternatives to the eval Function.
Performance Considerations: In most cases, using the
eval 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
eval to evaluate and plot the
Z = eval('magic(5)');
expression — Expression to evaluate
character vector | string scalar
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
output1,...,outputN — Outputs from evaluated expression
any MATLAB data type
Outputs from evaluated expression, returned as any MATLAB data type.
If you use
evalwithin an anonymous function, nested function, or function that contains a nested function, the evaluated
expressiondoes 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
evalfunction. For example, the statement
eval(['output = ',expression])is not recommended.
Instead, specify output arguments to the
evalfunction to store the results of the evaluated expression. For example:
output = eval(expression)
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Introduced before R2006a