Short, simple programs are easy to read, but as they get bigger and more complex, it gets harder to figure out what they do and how. That’s what comments are for.
A verbatim is a line of text added to a program to explain how it works. It has no effect on the execution of the program; it is there for human readers. Good comments make programs more readable; bad comments are useless at best and misleading at worst.
To write a comment, you use the percent symbol (%) followed by the text of the comment.
>> speed_of_light = 3.0e8 % meters per second speed_of_light = 300000000
The comment runs from the percent symbol to the end of the line. In this case it specifies the units of the value. In an ideal world, MATLAB would keep track of units and propagate them through the computation, but for now that burden falls on the programmer.
Avoid comments that are redundant with the code:
>> x = 5 % assign the value 5 to x
Good comments provide additional information that’s not in the code, like units in the example above, or the meaning of a variable:
>> p = 0 % position from the origin in meters >> v = 100 % velocity in meters / second >> a = -9.8 % acceleration of gravity in meters / second^2
If you use longer variable names, you might not need explanatory comments, but there’s a trade-off: longer variable names are clearer, but longer code can become harder to read. Also, if you’re translating from math that uses short variable names, it can be useful to make your program consistent with your math.