eval

Purpose

Interpret strings containing MATLAB expressions.

Synopsis

x = eval('string')
eval('string')

Description

x = eval('string'), where string is a MATLAB text string, causes MATLAB to interpret the string as an expression or statement.

eval('string'), where string is an expression, returns the value of that expression. If string represents a statement, eval('string') executes that statement. string is often created by concatenating substrings and variables inside square brackets.

Examples

The statements

s = '4*atan(1)';
pi = eval(s)
simply reset the value of pi.

The following loop generates a sequence of 12 matrices named M1 through M12:

for n = 1:12
    eval(['M',int2str(n),' = magic(n)'])
end
The next example runs a selected M-file script. Note that the strings making up the rows of matrix D must all have the same length.

D = [ 'odedemo '
      'quaddemo'
      'zerodemo'
      'fitdemo '];
n = input('Select a demo number: ');
eval(D(n,:))
The final example reads and processes the data in several files with the names data1.dat, data2.dat, and so on.

k = 0;
while 1
    k = k+1;
    datak = ['data' int2str(k)];
    filename = [datak '.dat'];
    if ~exist(filename), break, end
    eval(['load ' filename]);
    X = eval(datak);
    % Process data in matrix X.
end

See Also

feval

(c) Copyright 1994 by The MathWorks, Inc.