x = eval('string')
eval('string')
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.
simply reset the value ofs = '4
*atan(1)';
pi = eval(s)
pi
.
The following loop generates a sequence of 12 matrices named M1
through M12
:
The next example runs a selected M-file script. Note that the strings making up the rows of matrixfor n = 1:12
eval(['M',int2str(n),' = magic(n)'])
end
D
must all have the same length.
The final example reads and processes the data in several files with the namesD = [ 'odedemo '
'quaddemo'
'zerodemo'
'fitdemo '];
n = input('Select a demo number: ');
eval(D(n,:))
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
feval
(c) Copyright 1994 by The MathWorks, Inc.