Desktop Tools and Development Environment |
Comments in an M-file are strings or statements that do not execute. Add comments in an M-file to describe the code or how to use it. Comments determine what text displays when you run help
for a filename. Use comments when testing your files or looking for errors--temporarily turn lines of code into comments to see how the M-file runs without those lines. These topics provide details:
Commenting Using the MATLAB Editor/Debugger. You can comment the current line or a selection of lines:
A comment symbol, %
, is added at the start of each selected line, and the color of the text becomes green or the color specified for comments--see Syntax Highlighting.
To uncomment the current line or a selected group of lines, select Uncomment from the Text menu, or right-click and select it from the context menu. Comment to make those lines become comments in the M-file. -->
Commenting Using Any Text Editor. You can make any line a comment by typing %
at the beginning of the line. To put a comment within a line, type %
followed by the comment text; MATLAB treats all the information after the %
on a line as a comment.
To uncomment any line, delete the comment symbol, %
.
To comment a contiguous group of lines, type %{
before the first line and %}
after the last line you want to comment. This is referred to as a block comment. The lines that contain %{
and %}
cannot contain any other text. After typing the opening block comment symbol, %{
, all subsequent lines assume the syntax highlighting color for comments until you type the closing block comment symbol, %}
. Remove the block comment symbols, %{
and %}
, to uncomment the lines.
This examples shows some lines of code commented out. When you run the M-file, the commented lines will not execute. This is useful when you want to identify the section of a file that is not working as expected.
You can easily extend a block comment without losing the original block comment, that is, create a nested block comment, as shown in the following example.
Commenting Out Part of a Statement. To comment out the end of a statement, put the comment character, %
, before the comment. When you run the file, MATLAB ignores any text on the line after the %
.
To comment out text within a multiline statement, use the ellipsis (...
). MATLAB ignores any text appearing after the ...
on a line and continues processing on the next line. This effectively makes a comment out of anything on the current line that follows the ...
. The following example comments out the Middle Initial
line.
MATLAB ignores the text following the ...
on the line
Note that Middle Initial
is green, which is the syntax highlighting color for a comment.
MATLAB continues processing the statement with the next line
Formatting Comments. To make comment lines in M-files wrap when they reach a certain column,
To automatically limit comment lines to the maximum width while you type, select the Comment formatting preference to Autowrap comments.
For example, assume you select Autowrap comments and set the maximum width to be 75 characters, which is the width that will fit on a printed page using the default font for the Editor/Debugger. When typing a comment line, as you reach the 75th column, the comment automatically continues on the next line.
Entering Statements | Changing the Case of Selected Text |
© 1994-2005 The MathWorks, Inc.