Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do end-of-line commas do in Matlab?

Tags:

matlab

This is hard to look up: what do the end-of-line commas do in Matlab? In the couple of small tests I've done, they don't seem to make the code behave any different. I'd like to know because they're all over in this code I didn't write (but have to maintain).

Examples of what I mean:

if nargin<1,
    % code
end

if isError,
    % code
end

try,
    % code
    while 1,
        % even more code
    end
catch,
    % code
end
like image 621
Benjamin Oakes Avatar asked Feb 08 '10 15:02

Benjamin Oakes


People also ask

What does the comma do in MATLAB?

According to the documentation for the comma character in MATLAB, one of its functions is to separate statements within a line. If there is only one statement on a line, the comma is not needed. I don't like to see it there, although I know some people write code that way.

What does line continuation mean in MATLAB?

Uses: Line continuation Description: Three or more periods at the end of a line continues the current command on the next line. If three or more periods occur before the end of a line, then MATLAB ignores the rest of the line and continues to the next line.

Do you put commas at the end of a line?

If there is only one statement on a line, the comma is not needed. I don't like to see it there, although I know some people write code that way. As others have pointed out, commas at the end of a line are unnecessary.

Why does MATLAB ignore the rest of the line?

If three or more periods occur before the end of a line, then MATLAB ignores the rest of the line and continues to the next line. This effectively makes a comment out of anything on the current line that follows the three periods.


1 Answers

As others have pointed out, commas at the end of a line are unnecessary. They are really just for separating statements that are on the same line. mlint and the Editor will even give you a warning if you use one without needing it:

enter image description here

>> mlint comma_test.m
L 1 (C 4): Extra comma is unnecessary.
like image 94
gnovice Avatar answered Oct 27 '22 08:10

gnovice