开发者

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

开发者 https://www.devze.com 2022-12-19 07:01 出处:网络
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 a

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


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.


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:

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

>> mlint comma_test.m
L 1 (C 4): Extra comma is unnecessary.


If you read tightly coded m-files (e.g., many of the built-in MATLAB functions) you will discover a variant of the if ... end construct that is written on one line. Here's an example
if x<0, disp('imaginary'); end
Notice the comma between the x<0 and the disp(...). Apparently the comma tells the MATLAB interpretter that the conditional test has ended. To my knowledge this is only place where a statement (OK, part of a statement) ends with a comma. It's just one of those quirks that true believers come to use without hesitation.

http://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html


I think the comma in matlab is like the semicolon in C. It separates commands, so you can put multiple commands in one line separated by commas.

The way your program is written, I believe the commas make no difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消