How to run code from a later line multiple times without restarting the whole script?

22 visualizzazioni (ultimi 30 giorni)
I have a script that is over a hundred lines long. For sake of argument, lets say lines 1 to 100 take 30 seconds to process.
I am currently working on lines 101 - 130. This 30 line block of code relies on the data that is processed from lines 1 to 100. I am new to MATLAB, so a lot of my coding from lines 101 - 130 encounters errors frequently.
The issue is I have to re-run the code from line 1 just to see if a new change I've made to lines 101 to 130 actually works.
I have tried using breakpoints by setting a breakpoint at line 100. But after making a change then continuing my script from line 100, I still have to run the code from line 1 if I encounter an error from line 100 to 130.
My question therefore, is can I somehow 'cache' the data from line 1 to 100? Is it therefore possible to run my code from line 101 to 130 multiple times over as I make constant changes through multiple errors without ever going back to line 1?

Risposta accettata

Paul
Paul circa 5 ore fa
See Create and Run Sections in Code for how to divide the script (assuming it really is a scipt and not a function) into sections.
Then you can run the first section (lines 1-100) once and the results will all be in the base workspace as normal. Then you can run the second section (lines 101-130), check those results, edit the second section, and rerun as much as you'd like, always working with the same results from the first section (assuming the second section doesn't change the results from the first section).

Più risposte (1)

Torsten
Torsten circa 7 ore fa
Spostato: Torsten circa 7 ore fa
Save the workspace variables of lines 1 - 100 in a .mat-file. On restart, load this .mat-file to continue and use the script starting at line 101.
  1 Commento
dpb
dpb circa 6 ore fa
Structure the script into pieces -- then the second, beginning at line 101 can load the .mat and go on. Save the first section as its own m-file.
Ideal would be to turn them into functions...

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by