Keep Matlab from stepping into built in functions during dbstop if error

I use dbstop error a lot when working in Matlab. A good portion of the time, a mistake causes errors to be thrown inside of built-in [m-file] functions, which then causes Matlab to stop execution and open the file. However, it's almost never helpful to debug inside of the built-in file, so this ends up disrupting my workflow. Might there be a way to set things up so that Matlab backs out of the built-in file in the debugger (never opening it), leaving me at the function call?


Although I've never found a way to tackle this problem properly, it's fairly easy to hack together a workaround:

  • Create a script containing something along 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 a shortcut to it in the MATLAB toolbar.

  • Then, whenever this problem occurs, you just click on your little shortcut, which will automatically take you to the first non-builtin function in the debug stack.


    Based on Rody's answer and feedback from Mathworks, this is the closest you can get at this point (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:

  • Open up the closest user function to the correct line.
  • Close the builtin function that opened when the error was thrown.
  • If the error happened only one level away from a user function, switch to that workspace.
  • The problem is that dbup() only works once - after the call, execution in the script stops. There's no function to switch to an arbitrary place in the stack.

    链接地址: http://www.djcxy.com/p/94710.html

    上一篇: C ++ inplace析构函数编译警告

    下一篇: 如果错误发生,请在dbstop期间让Matlab逐步进入内置函数