R2010a: eval -> output sometimes double, sometimes single
Afficher commentaires plus anciens
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,
1 commentaire
Oleg Komarov
le 15 Déc 2011
Not enough info, what's myExpression?
Réponses (1)
Jan
le 15 Déc 2011
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.
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!