To comment out a selection, select the lines of code, go to the Editor or Live Editor tab,
and press the button. You also can type Ctrl+R. To uncomment the selected lines
code, press the button or type Ctrl+T.
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']
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,
suppose that you have this lengthy text into a commented line.
% This is a program 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 tab and in the Edit section, press the button.
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')
The button is not available in the Live Editor or in MATLAB Online.
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, go to the Home tab and in the Environment section, click
Preferences. Select MATLAB > Editor/Debugger > Language, and adjust the
Comment formatting preferences.
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
Changing the comment formatting preferences is not supported in MATLAB Online.
Add Comments to Programs