Matlab’s self-hosting nature can sometimes be an inconvenience. Case in point: if you have dbstop if all error enabled, and you save a file in the editor, Matlab will usually pull you up short at a breakpoint in mdbstatus.

If you find your Matlab editor randomly pulling you up in mdbstatus for no good reason, this might be what’s happening.

This happens because mdbstatus is called automatically by Matlab, and in mdbstatus, in its local function localGetFileBreakpoints, it uses a try/catch for detecting the debugger status of a given file. And its catch regularly gets hit during normal usage. Unfortunate design.

Luckily, mdbstatus is implemented in M-code, and not as a builtin, so we can hack around it. Here’s a hack to fix it:

Locate Matlab’s mdbstatus.m file using which mdbstatus. Copy that to a user-local file in your Matlab documents directory. (This is at ~/Documents/MATLAB on macOS and “My Documents/MATLAB” on Windows (%USERPROFILE/Documents/MATLAB by default on recent Windows versions.))

In your new user-local mdbstatus.m, add this code at the beginning of of the localGetFileBreakpoints function:

% apjanke's custom modification
% Avoid triggering "dbstop if all error" breakpoints here. Otherwise you'll
% pick up a breakpoint most times you do a file save!
origDebuggerState = dbstatus('-completenames');
RAII.dbstop = onCleanup(@() dbstop(origDebuggerState));
dbclear if all error
% end apjanke's custom modification

That will locally disable dbstop if all error while mdbstatus is doing its try/catch work.

There’s nothing special about the RAII variable. It’s just a local struct to hold cleanup functions that will be called when the stack unwinds; RAII is my conventional variable name for that. (Though it’s probably really better called “SBRM”.)

Since you’re hacking an internal Matlab function, I’d recommend making a prominent note of it. Stick this big old comment banner at the top of your new mdbstatus.m so it’s apparent what’s going on.

%  =========================================================
%    NOTE: This is apjanke's custom modification of mdbstatus.m, not the
%    original Matlab file. It has been modified to avoid triggering the
%    "dbstop if all error" breakpoint when files are saved in the editor.
%  =========================================================

(And use your own name if you want, of course.)

This was tested on Matlab R2016b and R2017b. YMMV on other versions. I’ve reported this behavior as a but to MathWorks, but who knows if and when they’ll get around to fixing it.