MATLAB Answers

14

How do I comment out a large block of code in MATLAB?

Asked by Doug Hull on 18 Jan 2011
Latest activity Edited by MathWorks Support Team on 8 Nov 2018
I want to do this in an easy way.

  1 Comment

Sign in to comment.

Products


Release

R2010b

4 Answers

Answer by Doug Hull on 18 Jan 2011
Edited by MathWorks Support Team on 8 Nov 2018
 Accepted Answer

To comment out a large block of code in the Editor or Live Editor, select the code and on the "Editor" or "Live Editor" tab, click the "Comment" button. This inserts a "%" symbol in front of each selected line. Alternatively, select the code and type "Ctrl" + "R". To uncomment the selected text, click the "Uncomment" button or type "Ctrl" + "T".
You also can use the block comment operators "%{" and "%}". The "%{" and "%}" operators must appear alone on the lines that immediately precede and follow the block of help text. For example:
%{
Stuff to be commented out
%}

  1 Comment

Also, the control-r and control-t work for C source code as well ... putting and removing the // style comments.

Sign in to comment.


Answer by CHUANQIANG ZHANG on 10 May 2018

Ctrl + R comment selected lines. Ctrl + T uncomment select lines.
https://stackoverflow.com/questions/30925796/is-there-a-shortcut-key-to-comment-multiple-lines-in-matlab-editor/30926004#30926004

  0 Comments

Sign in to comment.


Answer by Wesam Rezk on 30 Mar 2018

Simply start with *... and write whatever you want. This will enable you to write a very long comment line
For example *... This is a very very long comment

  0 Comments

Sign in to comment.


Answer by Wick
on 1 May 2018
Edited by Wick
on 1 May 2018

Rather than comment large sections of code, I bypass them with the following syntax.
for dont_go_here = []
% code I want to skip including all nested comments, etc. can stay here unmolested
end
This assumes that your if, for, case, try, and switch commands within that block are all terminated properly with an 'end' Otherwise the script won't parse.

  0 Comments

Sign in to comment.