Tutorial

Multiline Statements in MATLAB

A series of three consecutive periods is the line continuation operator in MATLAB.

Ellipsis

tells MATLAB to ignore the rest of the line, and continue the execution of the current statement to the next line. This operator is often referred as “the ellipsis”, and MATLAB interprets it as a space character.

You must use three consecutive periods. A single-character ellipsis will not work.

Examples

Use the ellipsis to create multiline statements, and to comment out parts of multiline statements.

% Sum numbers in multiple lines
y = 1 + ...
    2 + ...
    3;
% Create a matrix using one line per row
matrix = [...
    1, 2, 3; ...
    4, 5, 6; ...
    7, 8, 9 ...
    ];
% Build a long string
str = ['Visit the ImageProcessingLog.Com website and find calls for papers', ...
    ', news, jobs, tutorials, opinion articles, and exams about', ...
    ' digital image processing and related fields.'];
% Comment out parts of a multiline statement
newMatrix = [...
    1, 2, 3; ...
    ... 4, 5, 6; ...
    7, 8, 9 ...
    ];

Further reading

I recommend the following book to learn more on how the line continuation operator works 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 first chapter explains the line continuation operator, among other introductory topics like random numbers generation, trigonometry, and how to get help in MATLAB.

I also recommend the other tutorials in this series.

Source code

I hope you have learned how to use the line continuation operator 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.