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!