How to get Matlab stop on my file upon error?

212 views (last 30 days)
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
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
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...).

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 17 Apr 2022
Edited: Bruno Luong on 17 Apr 2022
The problem has been solved in R2022a.
Under R2021b here is the stack when debugger stops, one can see there is a dirty try/catch in the App callback engine that prevent the dbstop occurs at user level. This try/catch is no longer in R2022a App callbcak engine.
There is then no workaround for R2021b (unless modify the App calback engine AppManagementService.m but is extremely dangerous)
>> app1
>> edit moose
Index in position 1 exceeds array bounds. Index must not exceed 10.
Error in moose (line 5)
m = a(11 , 1);
Error in app1/ButtonPushed (line 14)
moose
236 throw(callbackException);
K>> dbstack
> In appdesigner.internal.service/AppManagementService/tryCallback (line 236)
In matlab.apps.AppBase>@(source,event)tryCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 37)
In matlab.ui.control.internal.controller/ComponentController/executeUserCallback (line 427)
In matlab.ui.control.internal.controller/ComponentController/handleUserInteraction (line 381)
In matlab.ui.control.internal.controller/PushButtonController/handleEvent (line 86)
In appdesservices.internal.interfaces.controller/AbstractController/handleProxyViewEvent (line 279)
In appdesservices.internal.interfaces.controller.AbstractController>@(src,event)handleProxyViewEvent(obj,src,event) (line 207)
In appdesservices.internal.interfaces.view/AbstractProxyView/notify (line 117)
In appdesservices.internal.peermodel/PeerNodeProxyView/handlePeerEventFromClient (line 268)
In appdesservices.internal.peermodel.PeerNodeProxyView>@(varargin)obj.handlePeerEventFromClient(varargin{:}) (line 81)
In viewmodel.internal.factory.ManagerFactoryProducer>@(src,event)callback(src,viewmodel.internal.factory.ManagerFactoryProducer.convertStructToEventData(event)) (line 105)
K>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.11.0.1873467 (R2021b) Update 3
MATLAB License Number: xxxxxx
Operating System: Microsoft Windows 8.1 Pro Version 6.3 (Build 9600)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.11 (R2021b)
MATLAB Coder Version 5.3 (R2021b)
MATLAB Compiler Version 8.3 (R2021b)
Optimization Toolbox Version 9.2 (R2021b)
Signal Processing Toolbox Version 8.7 (R2021b)
K>>
Here is the same output with R2022a, it stops on moose.m
> app1
>> edit moose.m
>> dbstop if error
Index in position 1 exceeds array bounds. Index must not exceed 10.
Error in moose (line 5)
m = a(11 , 1);
Error in app1/ButtonPushed (line 14)
moose
Error in appdesigner.internal.service.AppManagementService/executeCallback (line 138)
callback(appOrUserComponent, event);
Error in matlab.apps.AppBase>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 63)
newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...
5 m = a(11 , 1);
K>> dbstack
> In moose (line 5)
In app1/ButtonPushed (line 14)
In matlab.apps.AppBase>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 63)
K>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.12.0.1884302 (R2022a)
MATLAB License Number: xxxxxx
Operating System: Microsoft Windows 11 Home Version 10.0 (Build 22000)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.12 (R2022a)
MATLAB Coder Version 5.4 (R2022a)
MATLAB Compiler Version 8.4 (R2022a)
Optimization Toolbox Version 9.3 (R2022a)
Signal Processing Toolbox Version 9.0 (R2022a)
K>>

More Answers (1)

Matt J
Matt J on 16 Apr 2022
Edited: Matt J on 16 Apr 2022
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
Bruno Luong
Bruno Luong on 18 Apr 2022
BTW, besides restarting MATLAB, any way to undo this command?
dbclear all
Matt J
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.

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!