Effacer les filtres
Effacer les filtres

How to run a code only up to a certain line?

183 vues (au cours des 30 derniers jours)
Adam Aberra
Adam Aberra le 26 Nov 2017
Modifié(e) : Marc le 4 Août 2022
I'm in the process of editing an m-file that is pretty lengthy. Instead of running the whole thing, I'd like to run only the first several lines while I make some tweaks.
Is there are a MATLAB equivalent to "exit" in Stata: basically getting MATLAB to run the m-file only up to a specific line in the code?
Thanks in advance for your help.

Réponse acceptée

Image Analyst
Image Analyst le 26 Nov 2017
Either set a breakpoint there (click on the - on the left margin) or put a return statement there.
return; % Exit script or return to calling routine.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 26 Nov 2017
If you know the line number, then at the command line you can
But inside the editor it is easier just to click on the dash that shows up just to the right of the line number and before the code itself. A single click will turn it red, indicating that a breakpoint is there. If there is an anonymous function on the line then you might be asked whether the breakpoint is intended to be in it or on the line itself.
Once the code has started running and you are stopped at a breakpoint, a third way is available: the "Run and Advance" section changes to "Step In" and "Step Out" and "Run to Cursor". You can click on a line and then click on "Run to Cursor" and the program will execute until the line you had clicked on.
Setting a breakpoint is a bit more secure than "Run to Cursor" in that if anything interrupts the "Run to Cursor" (such as a breakpoint) then the editor will forget that temporary breakpoint was set -- but it will not forget breakpoints you set through clicking on the dash.
  1 commentaire
Adam Aberra
Adam Aberra le 26 Nov 2017
Thank you, Walter. That works also.

Connectez-vous pour commenter.

Stefanie Schwarz
Stefanie Schwarz le 27 Oct 2021
Starting in R2021b, with our improved editor, there is also a new "Run to here" feature to run the code until to a certain line and pause. See:
  1 commentaire
Marc le 4 Août 2022
Modifié(e) : Marc le 4 Août 2022
It was nice in older versions that you could just add the word "break" into your code and it would stop there.... but that was removed and now there are much fancier features. Where do we find this information? I cannot find general editor features in the documentation (just explicit features).

Connectez-vous pour commenter.


En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by