Skip to Main Content Skip to Search
Product Documentation

eval - Execute MATLAB expression in text string

Syntax

eval(expression)
[output1,...,outputN] = eval(expression)

Description

eval(expression) evaluates the MATLAB code in the string expression. If you use eval within an anonymous function, nested function, or function that contains a nested function, the evaluated expression cannot create a variable.

[output1,...,outputN] = eval(expression) stores output from expression in the specified variables.

Tips

Input Arguments

expression

String that contains a valid MATLAB expression.

To include a numeric value in the expression, convert it to a string with int2str, num2str, or sprintf.

Output Arguments

output1,...,outputN

Outputs from the evaluated expression.

Examples

Variable Name Evaluation

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 call 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 magic(5).

See Also

assignin | evalc | evalin | feval | try

Tutorials

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS