Do not let Matlab go into built-in functions during dbstop if error

I use dbstop error lot when working in Matlab. The good part of the time, the error causes errors to be built into the built-in [m-file] functions, which causes Matlab to stop executing and open the file. However, it is almost never useful to debug an embedded file, so this leads to disruption of my workflow. Could there be a way to adjust the situation so that Matlab backs away from the embedded file in the debugger (never opening it), leaving me on a function call?

+7
debugging matlab
source share
2 answers

Based on Rath's answer and reviews from Mathworks, this is the closest moment to you (R2016b):

 S = dbstack('-completenames'); builtins = ~cellfun('isempty', strfind({S(:).file}, matlabroot())); stack_depth = find(~builtins, 1, 'first'); hDocument = matlab.desktop.editor.findOpenDocument(S(1).file); matlab.desktop.editor.openAndGoToLine(S(stack_depth).file,S(stack_depth).line); hDocument.close(); if stack_depth == 2 dbup(); end 

This shortcut will be:

  • Open the closest user function to the correct line.
  • Close the built-in function that was opened on error.
  • If the error occurred only at one level from the user-defined function, switch to this workspace.

The problem is that dbup () only works once - after the call, execution in the script ends. There is no function to switch to any place on the stack.

0
source share

Although I have never found a way to solve this problem correctly, it is fairly easy to crack a workaround:

  • Create a script containing something in these lines:

     S = dbstack(); file_paths = cellfun(@which, {S.file}, 'UniformOutput', false); builtins = ~cellfun('isempty', strfind(file_paths, matlabroot())); stack_depth = find(~builtins, 1, 'first'); for ii = 1:stack_depth-1 dbup(); end 
  • Save it somewhere that makes sense to you, and place the shortcut on the MATLAB toolbar .

Then, whenever this problem occurs, you simply click on your small shortcut, which will automatically lead you to the first non-built-in function in the debug stack.

+4
source share