dbstop if error not working

I would like matlab to enter debug mode when there is an error by calling dbstop if error, but it's not working.
In the following code for example, Matlab errors as expected but it doesn't enter debug mode. Are there any settings I should check? I don't see a debug menu.
dbstop if error
ferror123()
function ferror123() % this function errors
x = [];
x(4);
end

2 件のコメント

Matt J
Matt J 2023 年 10 月 24 日
What happens if you use the Pause On Errors menu option instead of dbstop?
Dylan
Dylan 2023 年 10 月 24 日
That option doesn't seem to have any effect

サインインしてコメントする。

回答 (2 件)

the cyclist
the cyclist 2023 年 10 月 24 日

0 投票

ferror is the name of a built-in MATLAB function (which the debugger cannot open into). I expect it is being called, rather than your function.
Try renaming your function, and I expect the debugger will be triggered.

8 件のコメント

Dylan
Dylan 2023 年 10 月 24 日
Unfortunately that's not the problem, it's also not stopping on other custom functions I have. And I didn't know ferror was a built-in. I changed the name in this snippet to make it clear that's not the problem
the cyclist
the cyclist 2023 年 10 月 24 日
Hm. I changed the name of your function to frerror, and it triggered the debugger when I tried to run it.
So, to clarify, you now you have
function ferror123() % this function errors
x = [];
x(4);
end
stored in a file name ferror123.m and you then run
dbstop if error
ferror123
?
Maybe try
dbclear all
dbstop if error
Then I would escalate to restarting MATLAB.
Then I would consider reinstalling MATLAB.
Dylan
Dylan 2023 年 10 月 24 日
編集済み: Dylan 2023 年 10 月 24 日
I don't have it stored as a standalone file, it's just defined within my code. Is that required for dbstop to work? Is there any way to have dbstop work in funcitons defined within the file I'm working in?
Update: I tried defining the function within a file ferror123.m and I still have the same problem
the cyclist
the cyclist 2023 年 10 月 24 日
I'm pretty confused about how you are defining your function, and how you calling your function.
Let me try to be crystal clear about what I am doing to try and replicate your problem. I am putting the following code into a script file called dbstopError.m
dbstop if error
ferror123()
function ferror123() % this function errors
x = [];
x(4);
end
I have attached that file, for convenience.
Then, I execute that script from the Command Window:
>> dbstopTest
When I do that, MATLAB does enter debug mode, and my Editor window looks like this.
This is all as I would expect.
Can you try that same test?
Dylan
Dylan 2023 年 10 月 24 日
編集済み: Dylan 2023 年 10 月 24 日
That actually does enter debug mode, but I typically run cells of code (split off by lines of %%) by using cmd + enter and that method is still not entering debug mode when I run your file. Doing "Run all sections" (either by clicking the green play button or by pressing cmd + option + enter) does enter debug mode.
So the problem seems to be specifically with command + enter. Is this expected behavior?
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
Yes, it is expected behavior for cell mode. cell mode extracts the text of the cell into a temporary .m file and runs that file as a script, but the logic needs to catch errors in order to be able to patch up the line numbers of any errors to refer to the original line numbers instead of to the line numbers relative to the extracted file.
Dylan
Dylan 2023 年 10 月 24 日
Ok I see. And just to confirm dbstop if caught error is expected to work in cell mode? That does enter debug mode for me when I do cmd + enter.
Walter Roberson
Walter Roberson 2023 年 10 月 25 日
Yes, dbstop if caught error is expected to work in cell mode -- but any line numbers reported might look strange.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2023 年 10 月 24 日

0 投票

if you are within a try/catch then use
dbstop if caught error

3 件のコメント

Dylan
Dylan 2023 年 10 月 24 日
That did work although I'm not using try/catch. How does that differ from dbstop if error?
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
dbstop if error is only for unhandled errors when no try/catch is in effect.
dbstop if caught error is for all error cases... including "expected" errors.
What I mean by expected errors is that in order to deal with compatibility issues or with the possibility of hardware that might or might not be present, it is not uncommon for internal routines to use try/catch protecting a statement that is needed if conditions are right but can be skipped if they are not right.
As an example sometimes it is easier to code
try
delete(OldObject);
catch ME
end
rather than to code testing to see if OldObject exists and is in a deletable state
Dylan
Dylan 2023 年 10 月 24 日
Thanks.
Any reason why dbstop if caught error would work and not dbstop if error?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDebugging and Analysis についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2023 年 10 月 24 日

コメント済み:

2023 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by