How do I use the help command to display multi-line comments?

조회 수: 655 (최근 30일)
Benjamin Wong
Benjamin Wong 2016년 11월 2일
댓글: Andrew Sandeman 2023년 11월 7일
Multi-line comments can be achieved using:
%{
random foobars
%}
However, whenever I place this at the top of a new script and try to call the help command, the output keeps on resulting in just:
{
Does this mean that the help command is limited to only single-line comments? I would assume that this was the case and call it a day but a textbook on MATLAB shows an example that prints out a multi-line comment using 'help'.
  댓글 수: 3
Benjamin Wong
Benjamin Wong 2016년 11월 3일
All of the content between "%{" and "%{" is all green. For example:
%{
conevol Computes volume of a cone.
Creates variables radius and height.
Result is assigned to variable volume and displayed.
The script was written by Blaise Pascal
on the year 1660
%}
All of that text would be displayed in green font to signify it being a comment so I don't think the problem is that.
Walter Roberson
Walter Roberson 2016년 11월 3일
Sigh. I feel so old some days, my culture lost in time, like tears in the rain.

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

채택된 답변

Abhinav Gurram
Abhinav Gurram 2016년 11월 7일
The ability to add block comments to the help text of a function is not available in MATLAB.
MATLAB considers the group of consecutive lines immediately following the H1 line and those beginning with a % sign to be the online help text for the function. The H1 line is the first help text line that consists of comment text. Therefore, the H1 line begins with a % sign. The first line without a % sign as the left-most character ends the help.
In this case, as a block comment using the operators %{ and %} is added, the lines inside the block do not start with a % sign. Therefore, MATLAB considers the %{ line as the end of the help.
As an alternative, use a % sign at the beginning of each line instead of block comments. You can comment multiple lines by selecting them and using Ctrl + R. To uncomment them, select the group of lines, and use Ctrl + T.
Hope this helps!
  댓글 수: 2
Aaron Kaw
Aaron Kaw 2020년 11월 20일
Yes this definitely helps.
But doesn't address ease of use. This multiline documentation feature exists in so many other languages already. The pain of manually adding a % for each newline I make gets irritating.
And using CTRL + R means we have to keep toggling with CTRL + T whenever we need to make major edits to the help documentation of each function/class/etc.
Additionally, CTRL + R places the comment symbol at the beginning of each line, not indented to the the needed position.
Andrew Sandeman
Andrew Sandeman 2023년 11월 7일
Building on the above comment by Aaron with an example, say you would like to document your function as closely as possible to how MATLAB documents it (down to the whitespacing and indentation), see example below:
%CAT Concatenate arrays.
% CAT(DIM,A,B) concatenates the arrays A and B along
% the dimension DIM.
% CAT(2,A,B) is the same as [A,B].
% CAT(1,A,B) is the same as [A;B].
%
% B = CAT(DIM,A1,A2,A3,A4,...) concatenates the input
% arrays A1, A2, etc. along the dimension DIM.
I currently struggle to have a quick way to do this, for the reasons in the above comment.
(MATLAB 2023a)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by