How to get Matlab stop on my file upon error?
Show older comments
Hi,
in old Matlab version I could use "stop on error" under "Run" commend. Execution would stop prior to executing a command that will result in error. Very usefull feature when trying to debug your code.
Currently I'm using MATLAB 2021b, and if error occurs it open usually an extra one/two MATLAB inner files (see snapshot attached).
Is there away to configure it somehow to work as in older versions? I want matlab to halt before the error, to have all the variables in the WS, etc..

32 Comments
Bjorn Gustavsson
on 14 Apr 2022
Most of the time the debug-setting:
>> dbstop if error
still works. It stops after the error has occurred though, so if it happens deep down in a function-call-sequence it might take you a number of dbup-calls to get where the problem has its roots.
Does this never work for your current version?
Mark Golberg
on 15 Apr 2022
Bjorn Gustavsson
on 16 Apr 2022
So you want the a setting for the debugging that puts you in the workspace of "the deepest of your functions" in the call-stack when the error occurs, or one step deeper? That might be an interesting feature. I've only ever accepted getting the debug-prompt at the line where the error occurs, which might be un-interestingly deep, but it will make it explicitly clear where the error is (since it might not be obvious at the point an internal matlab-function is called), and I'm ever only a few "dbup" calls away from my entry-point.
Bruno Luong
on 16 Apr 2022
Really? AFAIK Matlab always stop at the inner most mfile, even for "old" MATLAB.
One workaround is to put
try
call_stockfunction
catch
keyboard
end
in *your* code, then you can call manually call_stockfunction at the prompt or do something else to find out the issue.
Bjorn Gustavsson
on 16 Apr 2022
@Bruno Luong: maybe there are a few more levels of function-calls inside the internal m-functions so that one ends a few steps deeper in the function-call stack now than earlier? This is something I've "felt on a perhaps-basis".
Bruno Luong
on 16 Apr 2022
Edited: Bruno Luong
on 16 Apr 2022
Possibly, but then it's not the behavior of dbstop if error but due to change of MATLAB stock function hierarchy, or the way OP's code is called within or not an App from R2021b.
Just to make sure that OP doesn't imply the wrong source of the "problem".
Mark Golberg
on 16 Apr 2022
If you can, I would atttach a simple .mlapp file that reproduces the issue.
Otherwise, I agree with Bruno. I've noticed no big change in the way the standard debugger behaves since Matlab 2018, so it must be something specific either to the user-supplied app code, or to appdesigner itself.
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
@Mark Golberg "I do NOT want to see any deeper layer MATLAB internal function, such as the ones I've posted in my original questions."
This happens also with R2018 and maybe all other releases: it stops where the error occurs (within an mfile) so at the depest layer, regardless it is your code or TMW code. There is no such thing as ignoring TMW deep-layer.
Do you work withhAppDesigner in R2018?
Bruno Luong
on 16 Apr 2022
Edited: Bruno Luong
on 16 Apr 2022
"Does it demonstrate the problem now?"
No it's normal behavior, always was and is like this.
Mark Golberg
on 16 Apr 2022
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
"WOW! Seriously?"
Of course I'm serious (but might be wrong despite using MATLAB since 25 years).
Bruno Luong
on 16 Apr 2022
Edited: Bruno Luong
on 16 Apr 2022
"In 2018 I've worked with GUIDE"
Hah. I though so. GUIDE is not Appdesigner, some deeper layer is not an mfile, apple and orange. You can still use GUIDE with R2021b or R2022a, then you don't see breaking at the deaper layer.
Mark Golberg
on 16 Apr 2022
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
Edited: Bruno Luong
on 16 Apr 2022
"Why, if I call for the same function, via a pushButton, suddenly, those weird "AppBase.m" function popup and MATLAB doesn't halt on MY function."
May be because AppDesigner throw an error for some reason and with some purpose, but catch later, and if user does NOT use dbstop if error it does not make a break.
It is just another system than GUI.
"Is there really no work aroud for this?"
No, but you can always make an enhancementt request to TMW. I think it's neat but I doubt they would do it (possibly a lot of works for them).
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
"So Mathwork guys, went from GUIDE to AppDesigner, and ruined this super nice debug functionality"
I have 0 serious and complex app using App Designer. If that tell you something. Still they progress.
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
Yes still with GUIDE. For two reasons (not yours) : graphical performance wise and I use many features (undocumented) with Java (see Yair blog) and this is no workaround with AppDesigner.
Also the GUIDE that have mfile separated to figure file is a little easier for versioning, this is crucial when many peoples participale in the development.
What good is the "Pause on Error" if it's not stops on my line of code but goes to some inner layer and stops on error there?
It should be a simple matter for you to step a few layers up in the dbstack menu to your own code.
Though I agree in principle that it would be better if the debugger treated internal appdesigner code as binary code, this shouldn't render "Pause on Error" entirely ineffective to you.
Mark Golberg
on 16 Apr 2022
Bruno Luong
on 16 Apr 2022
@Mark Golberg From what you described, the error is caused by another thread runing in parallel to your program, your function is NOT on the stack where the error occurs (TBC).
Mark Golberg
on 16 Apr 2022
Bjorn Gustavsson
on 16 Apr 2022
@Mark Golberg: To step up/down in the function call-stack you simply type:
dbup % /dbdown
in the command-window. That way you can move up and down between the function work-spaces as they were at the moment of error. This makes it possible to inspect all variables at every level in the function-call-stack leading to the error. This is vital since it is not necessarily obvious how the function ended up causing an error. Dont rely on pushing buttons.
Also read the help and documentation on debug.
Mark Golberg
on 18 Apr 2022
Bjorn Gustavsson
on 18 Apr 2022
Well, in similar cases I just set:
dbstop in moose1
possibly with a line-number to stop at:
dbstop in moose1 at 5
and then I rerun everything with the same input - then I get the debug-prompt in that function (at line 5 (possibly also with an additional conditional)). That way, especially if you have set a good enough conditional expression for when to stop you get the debug-prompt just before the error occurs. This might be a solid bit fiddly if you have a very complex function moose1.
Best of luck.
Bruno Luong
on 18 Apr 2022
Edited: Bruno Luong
on 18 Apr 2022
"How can I get into "moose1.m" function WS where the error actually occured?"
You can't since the error of moose is embeded in try/catch block and it is retrow by the calling function. So there is no lower level in the stack when the debugger stops. You can however recall mose or invoke the callback in the try block, but this is not convenient and you are never sure too reproduce the same effect/error with your function that would not reproduce the same thing from call to call (for example if it's depend on some random generated data or persistent variable, global variable, timer, etc...).
Accepted Answer
More Answers (1)
If you execute at the command line,
>> dbstop if caught error
and then rerun the app, I think you will find that the debugger stops where you want.

3 Comments
Mark Golberg
on 18 Apr 2022
Bruno Luong
on 18 Apr 2022
BTW, besides restarting MATLAB, any way to undo this command?
dbclear all
Matt J
on 19 Apr 2022
also make the problem to stop at a whole pack of new "matlab internal issues" that I was completely transpernat to them previously
Don't see why that would happen, unless you have lots of your own try-catch statements in the user-supplied code.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
