Syntax bug R2011a: x ==y
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
In coming up with an answer to another question I was playing around and think I found a bug in R2011a in Linux. Can anyone explain
>> x = randi(2, 10, 1)-1; % Dummy data
>> x == 0;
>> x ==0;
??? Error: "x" was previously used as a variable,
conflicting with its use here as the name of a function or command.
See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use
Command Syntax" for details.
The 2nd and 3rd line differ in the presence (works) and absence (errors) of a space after ==. I cannot think of why the space matters. The error message also doesn't make sense to me. Is this reproducible? Is it a bug?
3 Commenti
Risposta accettata
Sean de Wolski
il 20 Set 2012
Modificato: Sean de Wolski
il 20 Set 2012
It is seeing ==0 as a string and x as a function. I.e. equivalent to:
x '==0';
This is not a bug. From the doc:
Space after an identifier, but not after a potential operator, implies a function call using command syntax. For example:
ls ./d
4 Commenti
Sean de Wolski
il 20 Set 2012
Modificato: Sean de Wolski
il 20 Set 2012
Hence it throws the error for the conflict (it wants to call x as a function but can't because it's a variable)
Also:
x('==0') is equivalent to >>x '==0' and apparently >>x ==0
Jan
il 5 Mar 2013
The different behavior between "x == 0" and "x ==0" might be a documented bug. The smartness of Matlab intelligent auto-parsing of the non-functional form exceeds the intuition of programmers. Now I'm unsure avour "x>0" and "x >=0" also.
The number of forum users struggeling with save(FileName) compared to save FileName decreases. I assume the newer documentation is better.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!