Adding Comments in Matlab Codes(Illustrated Expression)

Adding Comments in Matlab Codes(Illustrated Expression)

In general, the software languages have generally a special symbol that the coder can insert a comment about codes inside the code paper. This is a very useful thing in coding because other readers of these codes could understand much more easily what all code lines inside the whole code paper mean. Also, the Matlab coding language has a special symbol that programmers use as inserting comments inside the Matlab codes. Sometimes as you know, the codes inside Matlab® could be very intricate so understanding the meaning of code lines inside Matlab codes could be troublesome. Inserting a comment line inside your Matlab code could overcome this situation. Below, you could see how to add comments in Matlab.

Check: Recommended Books to Learn Matlab®

Which Symbol In Matlab® Makes a Comment Line?

The syntax of the comment in Matlab is very simple. You just need to add % before and after the comments that you want to add to Matlab. Check the examples below to understand the syntax of adding comments in Matlab.

a = 3; %the value of a
b = 5; %the value of b
c = a*b; %the multiplication of a and b

As you can see from the very basic code lines in Matlab®, the comment lines are produced by using the ‘%’ symbol. So with this symbol, you could insert your comments to your Matlab® codes like the example above. Comment lines are not affecting your Matlab® code and these comment codes are generally used for explanation of codes inside the Matlab code.

In this example, we explained the whole variables by using the ‘%’ symbol next to them in Matlab®. Another user of this Matlab® code could understand what the whole variables inside this Matlab® code mean.

Additional Examples of Matlab Comments

A = [3, 6, 54, 5];
%these are the elements of A vector% 

For example in this code of Matlab, we create a vector ‘A’ that has four variables. But the reader of this code may not know what it is. So, we added a comment line that explains what this code is. We put the ‘%’ sign at the end of the code and the beginning of the code.

Matlab compiler will not perceive this comment line as code. You can write whatever you want inside these symbols.

v = zeros(2,3)
z = 5.*v
d = 5.*zeros(4,5)
%we created a zeros matrix which has 2x3 dimensions and we multiplies it with the 5. You can also multiply it directly%

This is another coding example of Matlab comments. We explained how to create zeros matrices in Matlab. As you understand, you can add whatever you want inside the comments in Matlab.

Matlab® provides various kinds of useful commands to use in mathematical and technical calculations. You can build code lines and codes to make the tough calcıulations easy. You can take a look at the useful commands in Matlab® below.

Conclusion

Do not forget to leave your comments and questions below about the comment lines in Matlab® below. 

If you want further coding examples about the comment lines in Matlab®, inform us in the comments.

We prepared this article for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.

Last Updated:

Author:


Leave a Reply

Your email address will not be published.