How to run code from a later line multiple times without restarting the whole script?
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Darrian Low
el 6 de Dic. de 2025 a las 7:37
Respondida: Paul
el 6 de Dic. de 2025 a las 13:05
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?
0 comentarios
Respuesta aceptada
Paul
el 6 de Dic. de 2025 a las 13:05
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).
0 comentarios
Más respuestas (1)
Torsten
el 6 de Dic. de 2025 a las 11:37
Movida: Torsten
el 6 de Dic. de 2025 a las 11:37
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 comentario
dpb
el 6 de Dic. de 2025 a las 12:40
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...
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!