Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

eval

Execute MATLAB expression in text

Syntax

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

Description

eval(expression) evaluates the MATLAB® code in the character vector 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.

Input Arguments

expression

Character vector that contains a valid MATLAB expression.

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

Output Arguments

output1,...,outputN

Outputs from the evaluated expression.

Examples

collapse all

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).

More About

collapse all

Tips

  • Many common uses of the eval function 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 syntax,

      output = eval(expression)

    allows the MATLAB parser to perform stricter checks on your code, preventing untrapped errors and other unexpected behavior.

See Also

| | | |

Introduced before R2006a


Was this topic helpful?