R2010a: eval -> output sometimes double, sometimes single

Hallo there,
do you have anyidea, when would eval output the same statement with double, and when as single ?
Because Im experiencing that
evalOut = eval(['round(myExpression)']);
is returning sometimes double, and sometimes single values. For me not deterministically.
Any idea why?
Best,

Answers (1)

If myExpression is a single and "round" is the function round, the output is a single also. But if "round" is a double array, the values of myExpression are interpreted as indices and the output has the type double.
So at first you have to check what "round" and "myExpression" is:
whos round
whos myExpression
In all possible cases this does not depend on eval! But you could use this to confuse Matlab - and its users:
round(3.14);
eval('round = 5');
eval('round(1)');
The behaviour can depend on the debug status of Matlab! Cruel. So I suggest to avoid this:
evalOut = eval(['round(myExpression)']);
under all circumstances and use this instead:
out = round(myExpression);
I do not know a reason why a program should be as confusing as possible. And as your question shows: With enough EVALs the behaviour of Matlab will look non-deterministic -- but of course it is not.

Categories

Asked:

on 15 Dec 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!