Updated May 31, 2023
Introduction to Matlab Block Comment
‘Block comment’ is a way in coding languages by which we comment a block or multiple lines of code to prevent the compiler from executing them. In MATLAB, we can comment a code which is as small as a single line and a code which is of hundreds of lines. Developers add various points to explain a code and make it easily understood by humans and thus making it more readable to the audience other than those involved in the development of the code. “Commenting” these lines ensure that the compiler does not execute them and prevents any potential error.
Syntax to comment a block of code:
1. By using the “Comment” button in the Live Editor (Present as a “%”)
2. By using the ‘%’ sign in the keyboard
3. By using the short cut keys – “Ctrl + R”
Let us now understand how to comment a block of code in MATLAB.
Examples
Let us discuss examples of Matlab Block Comment.
Example #1
In this example, we will use the “Comment” button in the Live Editor of MATLAB to comment a block of code. We will write a dummy code and will give some points explaining this code. We do not want these points to be executed by the MATLAB compiler and so will “comment” them. Below are the steps to be followed:
1. Write the code along with the explanation points
2. Select the block of code which you want to comment (points written as explanation)
3. Click on the “Comment” button (represented as a “%” sign) present in the “Live Editor” tab as shown below
Code:
nr = normrnd (0, 1, 1, 5)
A = quantile (nr, 0.15)
1. Initializing 5 normally distributed numbers
2. Passing the numbers in line 1 as input
This is how our input and output will look like in MATLAB:
Input:
Output (After commenting the block of code):
If we execute our input directly, MATLAB will throw an error for line 3 & 4 as these lines are in human-friendly language and not understood by the MATLAB compiler. We must “comment” these two lines to avoid the error. Here we have commented them by using the “Comment” button in the Live Editor of the MATLAB
Example #2
In this example, we will use the “%” key in our keyboard to comment a block of code. Here also we will write a dummy code and will give some points explaining this code. We do not want these points to be executed by the MATLAB compiler and so will “comment” them. Below are the steps to be followed:
1. Write the code along with the explanation points
2. Select the block of code which you want to comment (points written as explanation)
3. Add “%{“ in the beginning of the code to be commented
4. Add “%}” at the end of the code to be commented
Code:
A = mod (33, 5)
mod function gives remainder as the output
The output will be remainder of 33/5 i.e 3
This is how our input and output will look like in MATLAB:
Input:
Output (After commenting the block of code):
If we execute our input directly, MATLAB will throw an error for line 2 & 3 as these lines are in human-friendly language and not understood by the MATLAB compiler. We must comment these two lines to avoid error. Here, we have commented this block of 2 lines by using the ‘%’ key
Example #3
In this example, we will use shortcut keys in our keyboard to comment a block of code. For this, we will us a combination of “Ctrl” and “R” key on our keyboard. Here we will use the same code as in the above example. Below are the steps to be followed:
1. Write the code along with the explanation points
2. Select the block of code which you want to comment (points written as explanation)
3. Press “Ctrl” and “R” keys together
Code:
A = mod (33, 5)
mod function gives remainder as the output
The output will be remainder of 33/5 i.e 3
This is how our input and output will look like in MATLAB:
Input:
Output (After commenting the block of code):
As we can see in the output, the explanation points are now commented on. We have done this by pressing together “Ctrl” and “R” keys.
[Please note that, the output shown in above examples is not the output of the functions used in the code, but the result after commenting the block having explanation of the code]Conclusion
1. Block comment is used in MATLAB if we want to prevent a particular block of code from getting executed
2. This is usually done to provide the explanation of the code without interfering with the compiler
3. There are various ways in which we can comment a block of code in MATLAB
Recommended Articles
This is a guide to Matlab Block Comment. Here we discuss introduction, syntax, and how to comment a block of code in MATLAB with examples. You may also have a look at the following articles to learn more –