This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Add Comments to Programs

When you write code, it is a good practice to add comments that describe the code. Comments allow others to understand your code, and can refresh your memory when you return to it later.

Add comments to MATLAB® code using the percent (%) symbol. Comment lines can appear anywhere in a program file, and you can append comments to the end of a line of code. For example,

% Add up all the vector elements.
y = sum(x)           % Use the sum function.

In live scripts, you can also describe a process or code by inserting lines of text before and after code. Text lines provide additional flexibility such as standard formatting options, and the insertion of images, hyperlinks, and equations. For more information, see Create Live Scripts in the Live Editor.


When you have a MATLAB code file (.m) containing text that has characters in a different encoding than that of your platform, when you save or publish your file, MATLAB displays those characters as garbled text. Live scripts (.mlx) support storing and displaying characters across all locales.

Comments are also useful for program development and testing—comment out any code that does not need to run. To comment out multiple lines of code, you can use the block comment operators, %{ and %}:

a = magic(3);

The %{ and %} operators must appear alone on the lines that immediately precede and follow the block of help text. Do not include any other text on these lines.

To comment out part of a statement that spans multiple lines, use an ellipsis (...) instead of a percent sign. For example,

header = ['Last Name, ',      ...
          'First Name, ',     ...
      ... 'Middle Initial, ', ...

The MATLAB Editor includes tools and context menu items to help you add, remove, or change the format of comments for MATLAB, Java®, and C/C++ code. For example, if you paste lengthy text onto a comment line, such as

% This is a program that has a comment that is a little more than 75 columns wide.
disp('Hello, world')
and then press the button next to Comment on the Editor or Live Editor tab, the Editor wraps the comment:
% This is a program that has a comment that is a little more than 75
% columns wide.
disp('Hello, world')

By default, as you type comments in the Editor, the text wraps when it reaches a column width of 75. To change the column where the comment text wraps, or to disable automatic comment wrapping, adjust the Editor/Debugger Language preference settings labeled Comment formatting.

The Editor does not wrap comments with:

  • Code section titles (comments that begin with %%)

  • Long contiguous text, such as URLs

  • Bulleted list items (text that begins with * or #) onto the preceding line

Preference changes do not apply in live scripts.

Related Examples

More About

Was this topic helpful?