Tutorial

Comments in MATLAB

A comment is a human-readable annotation in the source code of a computer program, generally ignored by compilers and interpreters.

Single-line comments

The symbol % begins a single-line comment in MATLAB.

% A single-line comment spans from the '%' symbol to the end of line
A = [1, 2, 3]; % This is a single-line comment too

Block comments

Comment out multiple lines by preceding each one with a % symbol, or enclose them between %{ and %}.

%{ 
A block comment spans from the '%{' delimiter
to the '%}' delimiter and can include multiple lines
%}
%{ and %} must appear alone in their own line.

Multiline statements

To comment out parts of a multiline statement use an ellipsis (i.e. ) at the beginning of the target lines.

y = 1 + ...
    ... 2 + ... This line is ignored
    3;

Further reading

I recommend the following books to learn more on comments in MATLAB:

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a 2017 Textbook Excellence Award. The third chapter explains the use of comments for documentation, and other introductory topics on MATLAB programming.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 3 explains the help feature, which uses comments extensively.

I also recommend the other tutorials in this series.

Source code

I hope you have learned how to comment out lines, blocks, and parts of multiline statements in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.