Comments are statements to describe code.

Generally, Comments are for programmers not for a compiler. At runtime, These are ignored by Matlab.

In Matlab, You can write a comment in different following ways.

  • Single line comments
  • Multi-Line comments
  • Shebang commands

It is always good to practice adding comments to the code for better readability and maintenance of

Matlab Comments Single line

Single-line comments are always written in a single line. It can be added as a new line or existing code lines as inline code.

  • It always starts with a percentage (%) symbol character and ends with a line break.
  • There is a space after a percentage symbol
  • a string of text that begins with a % symbol is ignored by the Matlab compiler.
  • It is a description or piece of text for a single line of code.
  • These comments are not required at the start at the beginning of the line but also be written in the middle or end of the line Syntax:
% These are single-line comments in the Matlab file

Example

hello.mlx

% This is a simple hello world program in matlab
disp("Hello World Example");

Learned single-line comments and how to write a multi-line comment.

matlab Multiline or block comments

These comments contain comment text spanning multiple lines.

These comments are written in multiple lines and are also called block comments.

Multi-line comments start with %{ followed by comments in multiple lines and end with %}.

Here is a way we can write a multi-line comment in Matlab

%{
    multi-line comments 1
    multi-line comments 2
    multi-line comments 3
    %}

This allows you to write multiple lines of code and be ignored by the compiler.

  • This always starts with %{ and ends with %} string span in multiple lines

Block-level comments in Ruby Example:

%{
Hello world sample program
Basic program to print hello world to console
First code to write in Ruby language
}%
disp("Hello world sample program")

you can also nest a block comment inside another block comment.

Matlab multiline comment

ellipse(…) is used to add the comment text in multiple lines.

Matlab compiler ignores the text after the ellipse.

msg = ['Welcome to'...
'my application,'...
...' Written by W3s'...
'simple program']

If you print the above variable msg, It outputs

Welcome to my application, a simple program