Using conditional breakpoints to print out value
조회 수: 3 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
Jonathan Sullivan
2013년 2월 12일
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
추가 답변 (2개)
per isakson
2013년 2월 12일
편집: per isakson
2013년 2월 12일
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.
댓글 수: 0
Matt J
2013년 2월 11일
편집: Matt J
2013년 2월 11일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!