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”
on page 19-6.
Note 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 and functions (.mlx) support storing and displaying
characters across all locales.
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.
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
button. You also can type Ctrl+R. To uncomment the selected lines code,
click the
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']
Add Comments to Code
18-3
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”
on page 19-6.
Note 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 and functions (.mlx) support storing and displaying
characters across all locales.
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.
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
button. You also can type Ctrl+R. To uncomment the selected lines code,
click the
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']
Add Comments to Code
18-3
