Categories
1. Basics of Matlab

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

matlab 1

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:

matlab 2

Output (After commenting the block of code):

matlab 3

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:

code

Output (After commenting the block of code):

output

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:

input

Output (After commenting the block of code):

output 1

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]

Leave a Reply

Your email address will not be published. Required fields are marked *