how to continue code in next line with comment between them

i have a long line code, that i want to specify each part of code by comment in above it. i use (...) at end of each part to continue code in next line.like this:
% line 1
a=b+c+f...
% line 2
+t+h+k;
but when i write comments between lines, matlab just calculate first line. now what should i do to write comment between break lines.

1 件のコメント

France Lotto
France Lotto 2022 年 10 月 3 日
編集済み: Steven Lord 2022 年 10 月 4 日
This example shows how to continue a statement to the next line using ellipsis (...).
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;
Build a long character vector by concatenating shorter vectors together:
mytext = ['Accelerating the pace of ' ...
'engineering and science'];
The start and end quotation marks for a character vector must appear on the same line. For example, this code returns an error, because each line contains only one quotation mark:
mytext = 'Accelerating the pace of ...
engineering and science'
An ellipsis outside a quoted text is equivalent to a space. For example,
x = [1.23...
4.56];
is the same as
x = [1.23 4.56];
[SL Removed link]

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2017 年 4 月 30 日
編集済み: Stephen23 2018 年 1 月 18 日

2 投票

All you need is an ellipsis on each line which continues on to another line:
a = b+c+f...
... line 2
+t+h+k;

4 件のコメント

John Hatrick
John Hatrick 2018 年 5 月 10 日
Thanks for the workaround, but it doesn't help my case. I want to comment lines on the fly inside a continued statement as follows:
cost = ...
+ a ...
% + b ...
+ c ...
;
This would be very helpful while I'm experimenting with different terms in my optimization cost function. Is there a way to do this?
Respectfully, John
Stephen23
Stephen23 2018 年 5 月 11 日
編集済み: Stephen23 2018 年 5 月 11 日
cost = ...
+ a ...
... + b ...
+ c;
Works for me. Ellipses and comments are two different things: if you want to continue the code onto a new line then you need to use an ellipsis.
Steven Lord
Steven Lord 2018 年 5 月 11 日
And in fact this looks very similar to the example in the documentation for ellipsis. See the row for ... (Dot dot dot or ellipsis) in the "Special Characters" table on this documentation page, particularly the second half of the Examples column. [I see Stephen already linked to that documentation page, but didn't explicitly reference that particular section of that table.]
John Hatrick
John Hatrick 2018 年 5 月 24 日
編集済み: John Hatrick 2018 年 5 月 24 日
Thanks!
Not quite as easy as ctrl-r, but I can work with it.

サインインしてコメントする。

その他の回答 (1 件)

Andrew Newell
Andrew Newell 2017 年 4 月 30 日
編集済み: Andrew Newell 2017 年 4 月 30 日

0 投票

You can have the comments beside each part instead:
a=b+c+f... % line 1
+t+h+k; % line 2
However, it is possible to over-comment code! Using meaningful names for variables is a way of making the code self-commenting.

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

2017 年 4 月 30 日

編集済み:

2022 年 10 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by