Execute MATLAB expression in text
[output1,...,outputN] = eval(expression)
the MATLAB® code represented by
If you use
eval within an anonymous function, nested
function, or function that contains a nested function, the evaluated
create a variable.
Character vector or string scalar that contains a valid MATLAB expression.
To include a numeric value in the expression, convert it to a character vector or string scalar.
Outputs from the evaluated
Select a matrix to plot at runtime.
This example requires that you have a matrix in the current workspace. For example:
aMatrix = magic(5);
Interactively request the name of a matrix to plot, and
eval to use its value.
expression = input('Enter the name of a matrix: ','s'); if (exist(expression,'var')) mesh(eval(expression)) end
If you type
aMatrix at the input prompt,
this code creates a mesh plot of
Many common uses of the
are less efficient and are more difficult to read and debug than other MATLAB functions
and language constructs. For more information, see Alternatives to the eval Function.
Whenever possible, do not include output arguments
within the input to the
eval function, such as
eval(['output = ',expression]). The preferred
output = eval(expression)
allows the MATLAB parser to perform stricter checks on your code, preventing untrapped errors and other unexpected behavior.