Syntax bug R2011a: x ==y

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

Thomas
Thomas 2012년 9월 20일
Same error on the Mac as well.. R2012b.
I think the space (absence or presence of which causes it to identified as a variable or a function as is described in the help section 'How MATLAB Recognizes Command Syntax'
Javier
Javier 2012년 9월 20일
편집: Javier 2012년 9월 20일
Hello Tom
You have to give an additional space in the second x definition. No error in Matlab R2012a for Mac
>> x = randi(2, 10, 1)-1; % Dummy data
>> x == 0;
>> x == 0; %differs from x ==0; or
>> x=randi(2, 10, 1)-1; % Dummy data
>> x==0;
>> x==0;
Daniel Shub
Daniel Shub 2012년 9월 20일
@Javier, but I am specifically asking about the odd case of x ==0 (with a space before, but not after, ==). I think of MATLAB as being generous about the usage of white space. This is a case where you need to follow a few rules.

댓글을 달려면 로그인하십시오.

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 9월 20일
편집: Sean de Wolski 2012년 9월 20일

4 개 추천

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

Daniel Shub
Daniel Shub 2012년 9월 20일
To be exact, I think it is really seeing it as x('==0'). The link in the error message provides some clues. For whatever reason it is calling x as a function with the "command" syntax (i.e., how functions like help, doc and edit are often used and how load should never be used). The documentation, however, I cannot make heads or tails of the documentation.
Daniel Shub
Daniel Shub 2012년 9월 20일
But the reference says "If you issue such a statement at the command line, MATLAB can access the current workspace and path to determine whether ls and d are functions or variables."
Sean de Wolski
Sean de Wolski 2012년 9월 20일
편집: Sean de Wolski 2012년 9월 20일
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
Jan 2013년 3월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by