EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab comment
 

Matlab comment

Updated March 14, 2023

Matlab comment

 

 

Introduction to Matlab comment

We use ‘Comment’ in coding languages to provide narration to our code or block some lines of codes from getting executed. In MATLAB, we can provide narration that ranges from a few lines to hundreds of lines as per our requirement. It is a good coding practice to add narration or comments to a code that ensures the code’s smooth and easy understandability. But to prevent these lines from being executed by the compiler, we must ‘comment’ these lines. “Commenting” these comments or narration lines prevents the compiler from executing them and throwing run time errors. In this topic, we are going to learn about Matlab comments.

Watch our Demo Courses and Videos

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

Syntax to provide comments in MATLAB:

There are 3 ways in which we can provide comments in MATLAB and prevent them from getting executed by the compiler:

  1. Utilizing the “Comment” button present in the MATLAB Editor (Labelled as “%”)
  2. Utilizing the ‘%’ sign present in our keyboard
  3. Utilizing the “Ctrl + R” short cut keys

Examples of Matlab comment

Let us now understand how to provide comments in MATLAB.

Example #1

In the first example, we will make use of MATLAB’s “Comment” button present in the Live Editor. But, first, let us write a dummy code and provide a narration that will explain our code. Our purpose is to prevent our narration lines from getting executed by the compiler, for which we will “comment” the narration lines. Below are the steps to be followed:

  1. Write the code along with the narration to describe it
  2. Select the narrative lines which we want to comment (the lines written to explain the code)
  3. Click the “Comment” button (labeled as “%” sign) present in MATLAB’s “Live Editor” tab, as shown in the image below

Matlab comment output 1

Code:

I = [12 14 16 12 16 3] E = exp (I)
[Step 1: Initialize the array] [Step 2: Pass the array above as an argument]

This is how our input and output will look like in MATLAB:

Input:

Matlab comment output 2

Output 1 (After commenting on the narration lines):

Matlab comment output 3

If we try to execute our code directly, we will get a run time error in lines to lines 3 & 4 as these lines are not understandable by MATLAB’s compiler. Therefore, we must use these lines as comments, i.e. “comment” these narrative lines, to avoid any error. In this example, we have used the “Comment” button present in the Live Editor for this purpose.

Output 2

Matlab comment output 4

Example #2

In the second example, we will be using our keyboard’s “%” key to comment on our narrative lines. In this example also we will be writing a dummy code with some narrative explaining the code. Our purpose is to prevent these narrative lines from getting executed by the compiler. Below are the steps to be followed:

  1. Write the code along with the narration to describe it
  2. Select the narrative lines which we want to comment (the lines written to explain the code)
  3. Prefix “%{“ before the narrative lines which we need to comment
  4. Suffix “%}” at the end of the narrative lines

Code:

I = [3 5 3 13 6 6] E = expint (I)
[Step 1: Initialize the array] [Step 2: Pass the array above as an argument]

This is how our input and output will look like in MATLAB:

Input:

output 5

Output 1 (After commenting on the narration lines):

output 6

If we try to execute our code directly, we will get a run time error in lines to lines 3 & 4 as these lines are not understandable by MATLAB’s compiler. Therefore, we must use these lines as comments, i.e. “comment” these narrative lines, to avoid any error. In this example, we have used the “%” key present on our keyboard.

Output 2

output 7

Example #3

In the third example, we will be using our keyboard’s shortcut keys to comment on our narrative lines. For this, a combination of “Ctrl” plus “R” is used. For this example, 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 narration to describe it
  2. Select the narrative lines which we want to comment (the lines written to explain the code)
  3. Press “Ctrl” plus “R” keys together

Code:

I = [3 5 3 13 6 6] E = expint (I)
[Step 1: Initialize the array] [Step 2: Pass the array above as an argument]

This is how our input and output will look like in MATLAB:

Input:

output 8

Output 1 (After commenting on the narrative lines):

output 9

As we can see in the output, the narrative lines are now commented. This is achieved by pressing together “Ctrl” & “R” on our keyboard

Output 2

output 10

[Please note that the outputs shown in all the above examples are not the actual outputs of the functions, but the outcomes of using comments in MATLAB]

Conclusion

  1. Comments are used in MATLAB to provide narrative lines for the code and prevent them from getting executed.
  2. Doing this prevents any run time errors from being thrown by the compiler as it does not understand human-friendly languages.

Recommended Articles

This is a guide to Matlab comments. Here we discuss how to provide comments in MATLAB along with the examples and outputs. You may also have a look at the following articles to learn more –

  1. Matlab Block Comment
  2. Matlab limit
  3. Plot graph Matlab
  4. Matlab Cell
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
EDUCBA

*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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW