Filter löschen
Filter löschen

dbstop within a condition

4 Ansichten (letzte 30 Tage)
Pete
Pete am 17 Aug. 2018
Kommentiert: Pete am 17 Aug. 2018
Looking to use dbstop if I get into an if statement, so that a break point will be added to next line. I'm presently doing this by the following:
if StoredTextSettings{end,8} < StoredTextSettings{end,7}
dbstop at 67
pause(0.001)
end
where the pause(0.001) is line 67. What I want to do is to have location set as a variable so that I can edit code without having to change the hard-coded line "at location". I want to have something like:
if StoredTextSettings{end,8} < StoredTextSettings{end,7}
PresentLine = MFileLineNr(); % Fex function to get present line number
dpstop at PresentLine + 2
pause(0.001)
end
This will enable the code to be modified before the hardcoded line number(s) and should I exit matlab and forget to enable my own break point (presently how I do things, or just let error on dbstop() then have to repeat the code run (having enabled pausing on errors), which is obviously terrible programming! I've tried:
dbstop if StoredTextSettings{end,8} < StoredTextSettings{end,7}
but this didn't work as I thought it would, and neither of the following get the desired result
dbstop in TestDBStop if StoredTextSettings{end,8} < StoredTextSettings{end,7}
dbstop in TestDBStop.m if StoredTextSettings{end,8} < StoredTextSettings{end,7}
Anyone any tips on how to implement what I want (encounter a condition, and then enable a break point in that condition when the line number may change)? Thanks in advance

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 17 Aug. 2018
Bearbeitet: Sean de Wolski am 17 Aug. 2018
I think keyboard() does exactly what you want. It just stops at that line in debug mode.
But to find the number, I'd consider doing a contains() on the rows of a file after reading it in with fileread().
So
T = contains(string(fileread(mfilename)));
rownum = find(contains(T, '<Stop Here>'));
Now the row is:
pause(0.01) % <Stop Here>
  1 Kommentar
Pete
Pete am 17 Aug. 2018
Thanks Sean, exactly what I wanted! For those who are looking at something similar, use the following in the code:
if StoredTextSettings{end,8} < StoredTextSettings{end,7}
keyboard
end
This then opens up a DB point and allows you to continue as before.
Amazingly simple!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by