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

I want to do this in an easy way.

3 comentarios

CTRL+R
Hamid Nourani
Hamid Nourani el 21 de Abr. de 2020
Great
ctrl+r
u can use both for single or multiple line.

Iniciar sesión para comentar.

 Respuesta aceptada

Doug Hull
Doug Hull el 29 de Ag. de 2023
Editada: MathWorks Support Team el 29 de Ag. de 2023
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" + "Shift" + “R”.  
On macOS systems, use “Command” + “/” to comment and “Command” + “Option” + “/” to uncomment. On Linux® systems, use “Ctrl” +”/” to comment and “Ctrl” + “Shift” + “/” to uncomment. 
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: 
%{ 
     code to be commented out 
%} 

5 comentarios

James Tursa
James Tursa el 3 de Feb. de 2011
Also, the control-r and control-t work for C source code as well ... putting and removing the // style comments.
THIS METHOD NO LONGER WORKS IN MATLAB 2020
%{
...
%}
it appears there is no way to block comment in MATLAB 2020. You MUST line comment every single line.
Very interesting way to downgrade for the new version.....
That functionality is still present in releases R2020a, R2020b, and R2021a. It is possible that something about your file or how you're writing these comment delimiters is preventing them from being interpreted as block comments. For instance, %{ and %} must appear alone on a line.
x = 1
x = 1
%{ start of block comment?
y = 2
y = 2
%}
z = 3
z = 3
%{
w = 4
v = 5
%}
Because of the text following the %{ before "y = 2" it is a regular comment, not a block comment. So we execute the line that creates the y variable.
Because there is no %{ associated with the %} before "z = 3" it too is a regular comment and the z variable is created.
The %{ and %} surrounding the lines defining the w and v do comprise a block comment, so those variables are not created.
whos
Name Size Bytes Class Attributes x 1x1 8 double y 1x1 8 double z 1x1 8 double
Can you show us a small code segment where you see %{ and %} not functioning as block comments?
In Matlab 2020, you have to use ctrl+/ (cmd+/ for mac) to comment a block and to uncomment you have to use ctrl+t (cmd+t for mac).

Iniciar sesión para comentar.

Más respuestas (3)

CHUANQIANG ZHANG
CHUANQIANG ZHANG el 10 de Mayo de 2018

15 votos

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

3 comentarios

you are making us fool bc
ctrl+t open new tab.....mf
Manik Sharma
Manik Sharma el 15 de Jun. de 2023
Ctrl+t opens new tab bc.
Steven Lord
Steven Lord el 15 de Jun. de 2023
Search the list of keyboard shortcuts for the word "comment" or for the key combination Ctrl+T and you should see the actions associated with commenting out code or that will be performed in various components of the MATLAB Desktop in the keyboard shortcut set you've told MATLAB to use.
In my MATLAB installation using the Windows keyboard shortcut set, Ctrl+T creates a new tab in the Help Browser but uncomments code when used in the Editor.

Iniciar sesión para comentar.

Wesam Rezk
Wesam Rezk el 30 de Mzo. de 2018

2 votos

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
Wick
Wick el 1 de Mayo de 2018
Editada: Wick el 1 de Mayo de 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.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 18 de En. de 2011

Editada:

el 29 de Ag. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by