can I disable entire sections of code temporarily

254 vues (au cours des 30 derniers jours)
Tom
Tom le 28 Mar 2012
Commenté : Robert Daly le 9 Juil 2024
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 commentaires
Kabilan Nedunchezian
Kabilan Nedunchezian le 2 Fév 2017
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 le 9 Juil 2024
Kabilan, this tip is gold! Thanks.

Connectez-vous pour commenter.

Réponse acceptée

kfir
kfir le 28 Mar 2012
You can mark this whole part and Ctrl+R. you can also work in cell mode, and run only the cells you want.
  4 commentaires
Miguel Ordoñez
Miguel Ordoñez le 31 Mar 2018
Does anyone know if there is a command similar to Ctrl + R for Mac OS? Thanks
Mung Suan Pau DUHLIAN
Mung Suan Pau DUHLIAN le 8 Août 2020
Modifié(e) : Mung Suan Pau DUHLIAN le 12 Déc 2021
  • Select the code/s you want to temporarily disable ( or comment) and press (Command and / )
  • To undo the disable (or discomment ) select the code/s and press (Command and T)
  • Hope this is relevant:).

Connectez-vous pour commenter.

Plus de réponses (2)

Daniel Shub
Daniel Shub le 28 Mar 2012
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 le 28 Mar 2012
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.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by