A bit of MATLAB syntax fun

Just now, instead of executing the following code at the command line
>> x = 7;
>> x >= 5
I did the following typo:
>> x = 7;
>> x >- 5
Can you guess what the output was?

1 Comment

Interesting. So command/function equivalence does not look for variables with the given name.

Sign in to comment.

Answers (3)

Matt J
Matt J on 17 Mar 2022
Unrecognized function x?
Image Analyst
Image Analyst on 17 Mar 2022
I'd say true. I think it ignores the space between the negative sign and the 5 and 7 is greater than -5 so the answer is true.

1 Comment

This is what happens in PostgreSQL, which is where I actually made this typo (and was surprised that it was not just a syntax error). It took me a little while to figure out that it was interpreting it as a unary minus sign.
Then I tried it out in MATLAB, and was again surprised at the result! (But it didn't take me as long to figure out what was going on.)

Sign in to comment.

Jan
Jan on 17 Mar 2022
Edited: Jan on 19 Mar 2022
No, I cannot guess it.
I'm even astonished, that copy&pasting ">> x = 7;" works inspite of the ">>". Is this documented?

5 Comments

Sorry for the confusion. I only meant to represent that I was at the command line. I did not type the >>.
Also, I wanted to obscure the editor giving a clue to the answer:
x = 7;
x >- 5
Matlab's tries to guess smartly, what the user meant. Allowing the non-functional form of commands must cause ambiguities. I ran into troubles sometimes, when this interpretation changes between versions and in consequence I avoid this in productive code. I even write
axes('NextPlot', 'add')
instead of the fragile
hold on
I've never had trouble with hold on, and haven't noticed any difference in that from version to version. So, in my experience, it has not been "fragile".
@Image Analyst: Yes, you are right. My formulation was misleading. hold on is not and have not been fragile. I meant other command using non-functional forms.
fullfile * * % Working:
ans = '*/*'
fullfile * p % Error:
Error using fullfile
Not enough input arguments.
I had a longer list posted in the forum, which I add here, if I find it. The interpretation of the non-functional form did change between some releases. Therefore I avoid the non-functional form although it is expected, that e.g. "hold on" will work reliably. After being tricked too often, I got stubborn.
Responding to your first comment, @Jan ... I had forgotten about the Dumb Mistakes thread, but I'm proud that I had the most-upvoted answer!

Sign in to comment.

Categories

Find more on Functions in Help Center and File Exchange

Tags

Asked:

on 17 Mar 2022

Commented:

on 20 Mar 2022

Community Treasure Hunt

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

Start Hunting!