can I disable entire sections of code temporarily

조회 수: 73 (최근 30일)
Tom
Tom 2012년 3월 28일
댓글: Steven Lord 2024년 12월 5일
Can I disable entire sections of code temporarily?
I have a large code that contains different scenarios. I'm working on one scenario and I'd like to disable the other sections, just while I'm working on that bit so that I can run the code quickly without having to wait for it to do all the other stuff too.
  댓글 수: 4
Kabilan Nedunchezian
Kabilan Nedunchezian 2017년 2월 2일
Hi,
Use commenting in a smart way!
the following will not be executed in your code
%{
x = ('this will not be executed')
%}
to make it executable just remove the first curly bracket to make it executable
%
x = ('this will be executed')
%}
Robert Daly
Robert Daly 2024년 7월 9일
Kabilan, this tip is gold! Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

kfir
kfir 2012년 3월 28일
You can mark this whole part and Ctrl+R. you can also work in cell mode, and run only the cells you want.
  댓글 수: 7
DGM
DGM 2024년 12월 5일
You should normally be able to open preferences>keyboard>shortcuts and set them as needed. I don't know if that works on MATLAB Online.
Steven Lord
Steven Lord 2024년 12월 5일
In MATLAB Online you're not able to customize keyboard shortcuts AFAIK, but hovering over the comment button in the Code section of the Editor tab in the toolstrip says that Ctrl+R is the shortcut to comment out the selected lines in the Editor. Ctrl+Shift+R is the command to un-comment.
But yes, if you're using Desktop MATLAB you can customize the keyboard shortcuts and make Comment whatever you want (within reason; if you were on Windows, I believe it handles Ctrl+Alt+Delete on its own before that reaches any application so you can't use that as your shortcut.)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Daniel Shub
Daniel Shub 2012년 3월 28일
You can enclose code in an if block
if false
...
end
or even better (since you can include invalid syntax) would be to use block comments
%{
...
%}
The nice thing with block comments is that you can comment the comment
%%{
...
%}
to reactivate the code. The only thing to be careful with is if you auto indent and/or autowrap the code in the block comment your line breaks can get screwed up.

Jason Ross
Jason Ross 2012년 3월 28일
Usually when I get to a place where the code has gotten this large, a little voice in my head starts screaming "PUT IT IN A FUNCTION" (or subroutine, or module, or whatever means the language I'm coding in has to compartmentalize a section of code). That way I can separate the overall program flow from the detailed bit of what each step of the program is doing. And then it becomes simple to control that flow with a comment or other conditional method.

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by