Why don't I need a comment character after an (...) ellipsis?

조회 수: 32 (최근 30일)
connor sousa
connor sousa 2024년 11월 14일 23:47
댓글: Stephen23 2024년 11월 15일 6:39
When debugging some code, I found that the following produces the 1x4 array that one would hope for, but does not require the start of comment character %:
a = [1, 2,... why don't I need % here?
3, 4]
a = 1×4
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What is this (...) being interpreted as and are there any other characters with the same behavior?
  댓글 수: 1
Stephen23
Stephen23 2024년 11월 15일 6:39
"What is this (...) being interpreted as and are there any other characters with the same behavior? "
See:
Only the ellipsis is defined to continue code execution on the next line.
"Why don't I need a comment character after an (...) ellipsis?"
Because by definition the ellipsis continues the code execution on the next line. Therefore anything that follows on the same line cannot be executable code.

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

채택된 답변

Steven Lord
Steven Lord 2024년 11월 15일 0:35
Because that's what the ellipsis is documented to do. From that documentation page: "The ellipsis symbol (...) 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. This behavior effectively makes a comment out of anything on the current line that follows the three periods."
Unless I'm forgetting something esoteric the ellipsis and the comment character % are the only special characters that treat everything after them on a line as a comment. The block comment characters %{ and %} need to be on a line of their own (or with only white-space characters) to be treated as block comments, so the example below wouldn't make a block comment.
x = 1:10; %{
y = 2;
%}

추가 답변 (1개)

Torsten
Torsten 2024년 11월 15일 0:07
이동: Torsten 2024년 11월 15일 0:08
Most probably there is the convention that ... is the symbol to connect two subsequent lines and that everything behind the ... appearing in the same line is ignored.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by