views:

33

answers:

3

Hello all,

I'm debugging a matlab script that takes ~10 minutes to run. Towards the end of the script I do some i/o and simple calculations with my results, and I keep running into errors. Is there a way to start matlab from a certain sport in a script after it exits with an error--the data is still in the workspace so I could just comment out all of the code up until the error point, but I'm wondering if anyone knows a better way to go about doing this without rerunning the entire script (the ultra-lazy/inefficient way)?

Thanks,

Colorado

+1  A: 

Double percent signs will enable 'cell mode' which lets you run little blocks of code in steps. Sounds like just what youre looking for.

Karl
Very helpful tip for general "unit" tests, thanks for the answer
C. Reed
+2  A: 

Yes, use dbstop. Type dbstop if error and then run your script. The minute it hits an error, it will create a breakpoint there and you're in the workspace of the script --- which means you can debug the error, save data ; anything you want! Here's a snippet from the documentation for dbstop if error --- there are other ways to do dbstop, so do check it out:

dbstop if error

Stops execution when any MATLAB program file you subsequently run produces a run-time error, putting MATLAB in debug mode, paused at the line that generated the error. The errors that stop execution do not include run-time errors that are detected within a try...catch block. You cannot resume execution after an uncaught run-time error. Use dbquit to exit from debug mode.

Jacob
However, once you arrive at the error, you can select the remaining code and choose `Evaluate Selection` from the context menu. Thus, you can run a fixed version of the line that threw the error, as well as running the rest of the code (which e.g. saves all the results to file)
Jonas
Thanks so much, this is saving me a lot of time
C. Reed
@user471949: No problem! It was an eye-opener for me as well.
Jacob
A: 

Check this question and my answer:

Is there a way how to run MATLAB script from specific line without GUI?

yuk