EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Block Comment
 

Matlab Block Comment

Updated May 31, 2023

Matlab Block Comment

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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]

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 –

  1. Nyquist Matlab
  2. Matlab limit
  3. Plot graph Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW