Main Content

Add Comments to Code

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. During code development and testing, you also can use comments to comment out any code that does not need to run.

In the Live Editor, you can insert lines of text before and after code to describe a process or 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.

Add Comments

To add comments to MATLAB® code, use the percent (%) symbol. Comment lines can appear anywhere in a code 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.

Comment Out Code

To comment out multiple lines of code, use the block comment operators, %{ and %}. 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.

For example:

a = magic(3);
%{
sum(a)
diag(a)
sum(diag(a))
%}
sum(diag(fliplr(a)))

To comment out a selection, select the lines of code, go to the Editor or Live Editor tab, and in the Code section, click the comment button . You also can type Ctrl+R. To uncomment the selected lines code, click the uncomment button or type Ctrl+Shift+R. On macOS systems, use Command+/ to comment and Command+Option+/ to uncomment. On Linux® systems, use Ctrl+/ to comment and Ctrl+Shift+/ to uncomment.

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, ', ...
          'Title']

Wrap Comments

By default, as you type comments in the Editor and Live Editor, the text wraps when it reaches a column width of 75. The Editor and Live Editor do not wrap comments with:

  • Section titles (comments that begin with %%)

  • Long contiguous text, such as URLs

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

To change where comment text wraps or to disable automatic comment wrapping, go to the Home tab and in the Environment section, click Settings. Select MATLAB > Editor/Debugger > MATLAB Language, and adjust the Comment formatting settings.

Before R2025a: Select MATLAB > Editor/Debugger > Language instead.

If you have existing comments that extend past the current column width, to automatically wrap the comment, go to the Editor or Live Editor tab, and in the Code section, click the wrap comments button . For example, suppose that you have this lengthy text into a commented line.

% This is a code file that has a comment that is a little more than 75 columns wide.
disp('Hello, world')
With the cursor on the line, go to Editor or Live Editor tab, and in the Code section, click the wrap comments button . The comment wraps to the next line:
% This is a code file that has a comment that is a little more than 75
% columns wide.
disp('Hello, world')

Checking Spelling in Comments

Since R2024a

You can check for spelling issues in comments. Words with a potential spelling issue are underlined in blue. To resolve the issue, click the word and select one of the suggested corrections. You also can choose to ignore the issue or add the flagged word to your local dictionary. To navigate between issues using the keyboard, use Alt+F7 and Alt+Shift+F7.

Spell checking is supported in US English for MATLAB code files (.m), live code files (.m and .mlx), and Markdown files. To remove words from your local dictionary, go to your MATLAB settings folder (the folder returned when you run prefdir) and edit the file dict/en_US_userDictionary.tdi.

By default, spell checking is on. If a file contains many unrecognized words, such as when not written in US English, MATLAB automatically turns spell checking off for that file. To manually turn spell checking off for a file, go to the View tab, and in the Review section, toggle the Spelling button off. To turn spell checking off by default for all files, on the Home tab, in the Environment section, click Settings. Select MATLAB > Editor/Debugger > Spelling and set Check spelling to Off. To change what text to spell check, in the Where to check spelling section, select or clear the available options. For more information, see Editor/Debugger Spelling Settings.

Before R2026a: Spell checking is off by default. To turn spell checking on, go to the View tab, and in the Review section, toggle the Spelling button on. In addition, spelling settings are not available and spell checking in Markdown files is not supported.

See Also

Topics

Teaching Resources