I was surprised today to discover that
A = {1,2,3}
and
B = {1,2,3,}
are both valid syntax in MATLAB. I would have expected the second statement to yield an error. As best as I can tell, they produce identical cell arrays (all([A{:}]==[B{:}])
returns true).
Is there a reason the second syntax is allowed? Is this a bug in the parser? Are A
and B
truly the same?
Intriguingly, the following is not allowed:
C = {1,2,3,,,}
These are more guesses, rather than an answer.
One could check the Symbol reference and find that the comma ,
can be used as
Command or Statement Separator
To enter more than one MATLAB command or statement on the same line, separate each command or statement with a comma:
for k = 1:10, sum(A(k)), end
In the line
B = {1,2,3,}
therefore an statement after 3
is expected, there is just }
, which means end of cell array, a valid statement.
The semicolon ;
has three official usages:
Array Row Separator
When used within square brackets to create a new array or concatenate existing arrays, the semicolon creates a new row in the array:
A = [5, 8; 3, 4]
Output Suppression
When placed at the end of a command, the semicolon tells MATLAB not to display any output from that command. In this example, MATLAB does not display the resulting 100-by-100 matrix:
A = ones(100, 100);
Command or Statement Separator
Like the comma operator, you can enter more than one MATLAB command on a line by separating each command with a semicolon. MATLAB suppresses output for those commands terminated with a semicolon, and displays the output for commands terminated with a comma.
In this example, assignments to variables A and C are terminated with a semicolon, and thus do not display. Because the assignment to B is comma-terminated, the output of this one command is displayed:
A = 12.5; B = 42.7, C = 1.25;
So in the line
x = {1,2,3,;5,6,7}
it follows the valid statement Array Row Separator after 3,
. Afterwards a new statement is expected, which in this case is the double 5. Valid.
Now consider the case
x = {1,2,3,;;;;4,5,6;;;}
As above after 3,
follows the statement Array Row Separator, and the statement after that is presumably the null statement - NOP borrowed from some underlying program core written in C, which basically means: do nothing. So after 3,;
follows three times "do nothing", before there comes the next statement. Makes no sense, as Matlab is telling you: Extra semicolon is unnecessary. - but is valid.
It also allows you pointless things like:
if true
;
end
And this is presumably also the reason why
C = {1,2,3,,,}
returns an error, because the comma ,
isn't a null statement, but after the first comma there is a statement expected.
The bottom line: it looks weird, but actually seems logic to me, as Matlab uses a lot of C-Code internally and considering the null statement, everything mentioned is valid syntax.
What about other langages?
Semi-colons used like x = [1,2,3,;;;;4,5,6;;;]
in Python are invalid, even in the intended Matlab clone numpy, unless wrapped into this uncommon syntax a = np.matrix('1,2,3;4,5,6')
.
a = np.matrix('1,2,3,;;;;4,5,6;;;')
would throw an error as well, as ;
is interpreted as Array Row Separator in any case, which makes the compiler complain about inconsitent row sizes.
However,
x = [1,2,3,]
is also valid syntax in Python
and IronPython
, as it is in VBScript
and Lua
as mentioned in mlepage's answer. What do all these languages have in common? They are all (more or less) scripting languages interpreted during runtime. It's not just Matlab. The excitement of the OP therefore remains without cause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With