Using conditional breakpoints to print out value

Hi, I'm interested if it would be possible to use/abuse(?) conditional breakpoints to print out the value of a variable to the command window. Sometimes I work with a codebase that is read-only, so I can't insert 'disp()' statements... Thanks!

 採用された回答

Jonathan Sullivan
Jonathan Sullivan 2013 年 2 月 12 日

1 投票

Yes sir. It takes a bit of creativity.
Here's an example. Let's print out "Hi" each time mod(ii,2) == 0.
iff(mod(ii,2),fprintf('Hi.\n') > 0,false)
This will pause the code execution. If you prefer to not pause the code execusion, try:
iff(mod(ii,2),fprintf('Hi.\n') == NaN,false)
Where iff is defined as follows:
function out = iff(cond,vtrue,vfalse)
if cond
out = vtrue;
else
out = vfalse;
end

10 件のコメント

Sean de Wolski
Sean de Wolski 2013 年 2 月 12 日
This still doesn't help with the read-only part of Eric's original question...
I've put in the enhancement request for this which would essentially be:
dbdisp(str,'in',file,'at',lineNum,'if','ii=2')
Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Great Jonathan, that's very similar to what I was trying too - I just completely forgot that FPRINTF can print to the screen instead of just files, and returns the string length!! Those two things are the keys for making this work. I think you don't even need to create the IIF function in advance if you don't want, instead you can define this right in the conditional breakpoint:
if exprIsTrue, fprintf('test') < 0, else false, end
or the non-pausing version:
if exprIsTrue, fprintf('test') >=0, else false, end
Thanks again for your wonderful inspiration :)
Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Sean, that would help me because I could create the IFF function anywhere on my path :)
Sean de Wolski
Sean de Wolski 2013 年 2 月 12 日
But you'd have to modify the file in to include iff()
Sean de Wolski
Sean de Wolski 2013 年 2 月 12 日
Ahh got it; the iff() is the criteria for a conditional breakpoint. But it will still stop - which I though you did not want?
Sean de Wolski
Sean de Wolski 2013 年 2 月 12 日
I am still not able to get it to not pause/display wrogn results because the fprintf() is always evaluated. What am I missing?
Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Never mind my solution, that doesn't work :)
Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Sean, try changing the conditional breakpoint's second argument to be something like
@() fprintf('test.\n') < 0
and then in IIF do
out = vtrue();
Sean de Wolski
Sean de Wolski 2013 年 2 月 12 日
So that it evaluates the anonymous function; that makes sense.
Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Actually here's a pretty clean way to do it without requiring the IIF function, it relies on the short-circuting behavior of the AND operator to only execute the FPRINTF when the first expression is true - just put this as the breakpoint conditional expression:
mod(i,2) && (fprintf('test.\n') < 0) % or use >=0 if you want it to pause.

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

その他の回答 (2 件)

per isakson
per isakson 2013 年 2 月 12 日
編集済み: per isakson 2013 年 2 月 12 日

1 投票

Yes, that is possible. See tracer4m in the file exchange.
The function invoked at the conditional break point shall always return true and not error. Printing should be fine. If the name of variable isn't known beforehand one may use whos/who.
Matt J
Matt J 2013 年 2 月 11 日
編集済み: Matt J 2013 年 2 月 11 日

0 投票

Once the code stops at breakpoint, you can highlight a variable or expression in the editor window. Then right-click and select "Evaluate Selection".
You should also be able to see the value of a variable by hovering the mouse over any occurrence of it in the editor.

1 件のコメント

Eric Sampson
Eric Sampson 2013 年 2 月 12 日
Hi Matt, yup I understand what you say - but I'm looking to have the variable's value printed to the command window each time that the condition is true (like a trace or log), without having to manually inspect the value each time. Cheers :)

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by