How to make assert() debug break?

9 ビュー (過去 30 日間)
Zohar
Zohar 2022 年 7 月 5 日
コメント済み: Zohar 2022 年 7 月 5 日
I attached my solution. Not sure why it's not the default behavior.
function assert1( b )
if nargin < 1
b = 0;
end
if ~b
disp( 'Assertion failed.' );
dbstack
if 0
dbstop in assert.m at 11;
dbclear in assert.m at 11;
else
ST = dbstack;
if length( ST ) > 1
ST = ST(2);
end
fl = ST.file;
ln = ST.line + 1;
cmd = [ 'dbstop in ' fl ' at ' int2str(ln) ];
eval( cmd )
end
end
end
  1 件のコメント
Jan
Jan 2022 年 7 月 5 日
What is the behavior of your code? It creates a breakpoint in the next line and Matlab will stop there.
What happens in nested functions, functions attached to a script and in anonymous functions?

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

採用された回答

Steven Lord
Steven Lord 2022 年 7 月 5 日
Based on Jan's description (I have not read the code) you'd also want to think about what this does if you call assert1 at the MATLAB prompt, on the last line of a file, on the last line of a function, inside an eval call, in a P-coded file (I think this one might work), via mexCallMATLAB in a MEX-file, in a deployed application, ...
IMO there are certain functions that inherently require interactivity and so users won't be surprised if those functions behave interactively. input, uigetfile, and keyboard are examples of these. But if you call a function that requires interactivity in a function that doesn't, or if you make a function require interactivity that users' mental model says shouldn't require interactivity, that can be annoying to the point of unusability.
Imagine if (for example) the plot function prompted users for the X and Y data every time it was called. You couldn't use it in a program you intended to run unattended. Is users' mental model for assert more like input (where you expect it to require interactivity) or more like plot (start it running and let it do its thing)? To me having assert drop into debug mode would be quite annoying.
Besides, there is a way to get assert to stop and drop you into debug mode without requiring any changes to the functionality of assert or changes to code that calls assert. Set an error breakpoint. If the assert triggers it will cause an error which will be caught by the error breakpoint.
  2 件のコメント
Jan
Jan 2022 年 7 月 5 日
編集済み: Jan 2022 年 7 月 5 日
Exactly: Typing this in the command window let each failing assert enter the debug mode:
dbstop if error
Or even inside TRY/CATCH:
dbstop if caught error
Then you can stay at the standard assert and perform your debugging dynamically.
Zohar
Zohar 2022 年 7 月 5 日
Much better, I'm happy with that.
Up until now, everytime something asserted, I needed to relace "assert(b)" with "if b, assert".
I was looking for a behavior like in c++: in debug mode, assert breaks, in release it's skipped. These options let me attune matlab in the same spirit.
Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by