MATLAB Answers


Can I comment a block of lines in an MATLAB file using /* ... */ as I can in C?

I understand that in MATLAB we can comment line by line using % but this is troublesome, especially if I have a whole group of lines that I want to comment out temporarily. Is there anyway we can 'block' comment, like in 'C' where we can use /* at the beginning of a block of comments and */ at the the end of the block?

2 Answers

Answer by MathWorks Support Team on 15 Nov 2016
 Accepted Answer

This enhancement has been made for Release 14 (R14). You can use the following syntax to comments a block of code in MATLAB release 14 (R14) onwards:
For previous product releases, please read below for any possible workarounds:
If you are using the MATLAB editor, you can highlight the block of code that you want to comment then select the "Text->Comment" menu item or type a Control-R to comment the whole block. To uncomment the block, once again select the block of code, select "Text->Uncomment" or type Control-T key combination. This functionality is also available through right-mouse-clicks.
You cannot comment your MATLAB code with /*....*/ as in C. However, the above workaround should allow you to accomplish the same goal, to comment an entire block.


One trick: on a source line, everything after a ... (that is not in a string) is treated as comments.
Just type your comment and highlight the block, after which just do (CTRL + R) that is it. To uncomment a block of code just use (CTRL + T).
I want to note a possible bug (maybe it's an intended feature) with this solution.
I've had issues when I have other text on the same line as the "%{"-character. It will ignore the block comment and still execute the code. It seems that leaving the "%{" and "%}" markers on their own lines prevents this issue.

Sign in to comment.

Answer by peter gitagama on 14 May 2018

%{ comments %}


Sign in to comment.