MATLAB Programming/Comments
Placing comments [ edit | edit source ]
Comment lines begin with the character ‘%’, and anything after a ‘%’ character is ignored by the interpreter. The % character itself only tells the interpreter to ignore the remainder of the same line.
In the MATLAB Editor, commented areas are printed in green by default, so they should be easy to identify. There are two useful keyboard shortcuts for adding and removing chunks of comments. Select the code you wish to comment or uncomment, and then press Ctrl-R ( ⌘ -/ for Mac) to place one ‘%’ symbol at the beginning of each line and Ctrl-T ( ⌘ -T for Mac) to do the opposite.
MATLAB also supports multi-line comments, akin to /* . */ in languages like C or C++, via the % < and %>delimiters. But there is a small and important difference. In MATLAB it is not allowed that the lines starting with % < or %>contains any other text (except white spaces). Otherwise it would not work. E.g.
gives an error, but
works just fine.
Common uses [ edit | edit source ]
Comments are useful for explaining what function a certain piece of code performs especially if the code relies on implicit or subtle assumptions or otherwise perform subtle actions. Doing this is a good idea both for yourself and for others who try to read your code. For example,
It is common and highly recommended to include as the first lines of text a block of comments explaining what an M file does and how to use it. MATLAB will output the comments leading up to the function definition or the first block of comments inside a function definition when you type:
All of MATLAB’s own functions written in MATLAB are documented this way as well.
Comments can also be used to identify authors, references, licenses, and so on. Such text is often found at the end of an M file though also can be found at the beginning. Finally, comments can be used to aid in debugging, as explained in Debugging M Files.
Is there a shortcut key to comment multiple lines in matlab editor
Is there a shortcut to comment multiple lines in matlab. If there isn’t can I create one?
3 Answers 3
The comment shortcut is CTRL+R on Windows and CTRL+/ on Unix systems (not sure about OS X). To comment multiple lines you’d have to highlight them using the mouse or SHIFT .
In any case this can be customized via Preferences -> Keyboard -> Shortcuts.
Matlab — Comments
This tutorial covers Matlab comments Single multi-line comments API documentation comments block comments with example.
Comments are statements to describe code.
Please enable JavaScript
Generally, Comments are for programmers not for a compiler. At runtime, These are ignored by Matlab.
- Single line comments
- Multi-Line comments
- Shebang commands
It is always good to practice adding comments to the code for better readability and maintenance of
Matlab Comments Single line
- It always starts with a percentage (%) symbol character and ends with a line break.
- There is a space after a percentage symbol
- a string of text that begins with a % symbol is ignored by the Matlab compiler.
- It is a description or piece of text for a single line of code.
- These comments are not required at the start at the beginning of the line, but also can write in the middle or end of the line Syntax:
Learned single line comments, How do you write a multi-line comments.
matlab Multiline or block comments
These comments contain comment text span in multiple lines.
These comments are written in multiple lines and are also called block comments.
MATLAB Language Useful tricks Comment blocks
If you want to comment part of your code, then comment blocks may be useful. Comment block starts with a % < in a new line and ends with %>in another new line:
This allows you fo fold the sections that you commented to make the code more clean and compact.
These blocks are also useful for toggling on/off parts of your code. All you have to do to uncomment the block is add another % before it strats:
Sometimes you want to comment out a section of the code, but without affecting its indentation:
Usually, when you mark a block of code and press Ctrl+r for commenting it out (by that adding the % automatically to all lines, then when you press later Ctrl+i for auto indentation, the block of code moves from its correct hierarchical place, and moved too much to the right:
A way to solve this is to use comment blocks, so the inner part of the block stays correctly indented: