| MATLAB Function Reference | Search  Help Desk |
| eval | Examples See Also |
Interpret strings containing MATLAB expressions
a = eval('expression')
[a1,a2,a3...] = eval('expression')
eval(string,catchstring)
a = eval('expression')
returns the value of expression, a MATLAB expression, enclosed in single quotation marks. Create 'expression' by concatenating substrings and variables inside square brackets.
[a1,a2,a3...] = eval('expression')
evaluates and returns the results in separate variables. Use of this syntax is recommended over:
eval('[a1,a2,a3...] = expression')
which hides information from the MATLAB parser and can produce unexpected behavior.
eval(string,catchstring)
provides the ability to catch errors. It executes string and returns if the operation was successful. If the operation generates an error, catchstring is evaluated before returning. Use lasterr to obtain the error string produced by string.
A = '1+4'; eval(A)
ans =
5
P = 'pwd'; eval(P)
ans =
/home/myname
The loop
for n = 1:12
eval(['M',int2str(n),' = magic(n)'])
end
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 matrix D must all have the same length.
D = ['odedemo '
'quaddemo'
'zerodemo'
'fitdemo '];
n = input('Select a demo number: ');
eval(D(n,:))
feval Function evaluation
lasterr Last error message.