Desktop Tools and Development Environment Previous page   Next Page

Adding Comments in M-Files

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:

  1. For a single line, position the cursor in that line. For multiple lines, click in the line and then drag or Shift+click to select multiple lines.
  2. Select Comment from the Text menu, or right-click and select it from the context menu.

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. -->

Image of Editor/Debugger showing how to comment a selected group of lines. Select the contiguous lines, then select Text ->

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.

Image of a line of code that begins with the comment symbol, %. MATLAB ignores that line, treating it as a comment. The same line, not preceded by a comment symbol, produces an error when you run the M-file.

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.

Image of block comment.

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.

Image showing nested block comments.

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 %.

Image of comment within a line. The statement is a = zeros(10) % Initialize matrix. Text after the % is considered to be a comment.

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.

Image of multiline statement, where one line is a comment. The first line is header = [\xd5 Last NAme, \xd5 ... The second line is \xd5 First Name, \xd5 ... The third line is ... \xd5 Middle Initial, \xd5 . The fourth line is \xd5 Title\xd5 ].

MATLAB ignores the text following the ... on the line

Image of third line. The third line is ... \xd5 Middle Initial, \xd5

Note that Middle Initial is green, which is the syntax highlighting color for a comment.

MATLAB continues processing the statement with the next line

Image of next line, \xd5 Title\xd5 ].

MATLAB effectively runs

Image of lines that MATLAB runs. First line is headers=[\xd5 Last Name, \xd5 ... Second line is \xd5 Frist Name, \xd5 ... Last line is \xd5 Title\xd5 ].

Formatting Comments.   To make comment lines in M-files wrap when they reach a certain column,

  1. Specify the maximum column number using preferences for the Editor/Debugger. Select Language -> M. For Comment formatting, set the Max width.
  2. Select contiguous comment lines that you want to limit to the specified maximum width.
  3. Select Text -> Wrap Selected Comments.
  1. The selected comment lines are reformatted so that no comment line in the selected area is longer than the maximum. Lines that were shorter than the specified maximum are merged to make longer lines if they are at the same level of indentation.

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.


Previous page  Entering Statements Changing the Case of Selected Text Next page

© 1994-2005 The MathWorks, Inc.