MATLAB Answers

Doug Hull

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

Asked by Doug Hull
on 18 Jan 2011

I want to do this in an easy way.




1 Answer

Answer by Doug Hull
on 18 Jan 2011
 Accepted answer

Starting in MATLAB 7 (R14), the syntax is:

   Stuff to be commented out

You can also highlight a section of code and type control-r to comment out the code -- this will place a percent symbol (%) at the beginning of the line. Typing control-t will uncomment the lines by removing any percent symbol that is the first non-blank character on the line.

If you have an older version, the built-in editor in MATLAB 6.0 has a block-comment feature, which will simply put a comment character on each line. Or you can use matlab-mode for Emacs, which supports this as well.

If you are using an even older version, use this:

if 0
  commented out code 

This is not the best solution, since parse errors inside that block will cause an error.

[From the MATLAB FAQ of Ancient Times]

  1 Comment

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

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!