Azzera filtri
Azzera filtri

dbstop within a condition

4 visualizzazioni (ultimi 30 giorni)
Pete
Pete il 17 Ago 2018
Commentato: Pete il 17 Ago 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

Risposta accettata

Sean de Wolski
Sean de Wolski il 17 Ago 2018
Modificato: Sean de Wolski il 17 Ago 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 Commento
Pete
Pete il 17 Ago 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Debugging and Analysis in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by