Skip to content
Related Articles

Related Articles

Improve Article

Comments in MATLAB

  • Last Updated : 20 Aug, 2020

Comments are generic English sentences, mostly written in a program to explain what it does or what a piece of code is supposed to do. More specifically, information that programmer should be concerned with and it has nothing to do with the logic of the code. They are completely ignored by the compiler and are thus never reflected on to the input.

In MATLAB, comments are of two types:

  • Single-line Comments
  • Block Comments
  • Spanning MUltiple Lines

Single-Line Comments

Single-line comments are comments that require only one line. They are usually drafted to explain what a single line of code does or what it is supposed to produce so that it can help someone to refer to the source code. Use % operator for adding single-line comments. This can be written in a separate line or appended to code on the same line.

Syntax :

% single line comment

Example :



MATLAB




disp("Below is the comment using %"); 
% this is a comment

Output :

Below is the comment using %

Block Comments

To make a block comment just put a ‘%{‘ in the starting of the block and ‘%}’ at the end of the block.

Syntax: 

%{
multiline
comment
%}

Example :

MATLAB




disp("Below is the block comment using %"); 
%{ 
this is a 
comment 
using % 
%}

Output :

Below is the block comment using %

Comments Spanning Multiple Lines

Commenting part of a statement spanning multiple lines uses ellipsis (…). 

Example :

MATLAB




%{
a = [1 2 3]
b = 5
%}
  
x = ['Matlab is a '...
'programming langauge'...
...'invented by Cleve Moler'...
', it is user friendly']

Output :

Matlab is a programming language, it is user friendly

To comment out the lines of code in a script, press Ctrl + R, to uncomment them press Ctrl + T.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :