<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Adding Debugging Code Without Changing Your Code</title>
	<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/</link>
	<description>Ken &#38; Mike work on the MATLAB Desktop team</description>
	<pubDate>Mon, 23 Nov 2009 01:28:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: kena</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-6001</link>
		<dc:creator>kena</dc:creator>
		<pubDate>Wed, 19 Nov 2008 20:37:08 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-6001</guid>
		<description>Brenda (@8) –
That is what I thought too, but it didn’t work for me.  I found I needed the ‘0==’.  I should double-check this with the original developers, but I believe that the conditional *must* evaluate to a logical value.  A “bare” FPRINTF returns a scalar, which the debugger complains about at run-time.</description>
		<content:encoded><![CDATA[<p>Brenda (@8) –<br />
That is what I thought too, but it didn’t work for me.  I found I needed the ‘0==’.  I should double-check this with the original developers, but I believe that the conditional *must* evaluate to a logical value.  A “bare” FPRINTF returns a scalar, which the debugger complains about at run-time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kena</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-6000</link>
		<dc:creator>kena</dc:creator>
		<pubDate>Wed, 19 Nov 2008 20:31:02 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-6000</guid>
		<description>Per (@6) –
Sure, this should not be a problem.  In fact, what you’re describing it closer to the original “intent” of conditional breakpoints.  Here is a conditional that prints out even loop iterations for a variable ‘i’:

mod(i,2)==0 &#038;&#038; 0==fprintf('%d\n', i) &#038;&#038; 0</description>
		<content:encoded><![CDATA[<p>Per (@6) –<br />
Sure, this should not be a problem.  In fact, what you’re describing it closer to the original “intent” of conditional breakpoints.  Here is a conditional that prints out even loop iterations for a variable ‘i’:</p>
<p>mod(i,2)==0 &#038;&#038; 0==fprintf(&#8217;%d\n&#8217;, i) &#038;&#038; 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kena</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5999</link>
		<dc:creator>kena</dc:creator>
		<pubDate>Wed, 19 Nov 2008 20:24:24 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5999</guid>
		<description>Per (@5) --

You are correct, breakpoints are lost when you close MATLAB.  An option to automatically save state is on our long to-do list.  You can save and restore them programmatically using a dbstatus/save/load/dbstop sequence – search the help for “Restore Saved Breakpoints” for details.

If you want to determine if a function was hit during execution or not, using the profiler as a coverage tool is probably the most straight-forward approach.  After you’ve run your program with the profiler on, something like this will give you a list of functions called:

 I = profile('info');
 fList = sort({I.FunctionTable(:).FunctionName});

There is also the Coverage Report (“Identifying How Much of an M-File Ran When Profiled” in help), but it is more line-centric than function-centric.

All of that said, you should be able to troll through a file with regexp to pull out the various function declarations and set breakpoints at each, though I have not personally done this.</description>
		<content:encoded><![CDATA[<p>Per (@5) &#8211;</p>
<p>You are correct, breakpoints are lost when you close MATLAB.  An option to automatically save state is on our long to-do list.  You can save and restore them programmatically using a dbstatus/save/load/dbstop sequence – search the help for “Restore Saved Breakpoints” for details.</p>
<p>If you want to determine if a function was hit during execution or not, using the profiler as a coverage tool is probably the most straight-forward approach.  After you’ve run your program with the profiler on, something like this will give you a list of functions called:</p>
<p> I = profile(&#8217;info&#8217;);<br />
 fList = sort({I.FunctionTable(:).FunctionName});</p>
<p>There is also the Coverage Report (“Identifying How Much of an M-File Ran When Profiled” in help), but it is more line-centric than function-centric.</p>
<p>All of that said, you should be able to troll through a file with regexp to pull out the various function declarations and set breakpoints at each, though I have not personally done this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brendan Hannigan</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5998</link>
		<dc:creator>Brendan Hannigan</dc:creator>
		<pubDate>Wed, 19 Nov 2008 20:04:06 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5998</guid>
		<description>For the simple case I think You don't even need the leading "0 == ", but instead can just do:

fprintf('My message\n') &#38;&#38; 0

it's a bit tidier.</description>
		<content:encoded><![CDATA[<p>For the simple case I think You don&#8217;t even need the leading &#8220;0 == &#8220;, but instead can just do:</p>
<p>fprintf(&#8217;My message\n&#8217;) &amp;&amp; 0</p>
<p>it&#8217;s a bit tidier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5997</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Wed, 19 Nov 2008 19:50:30 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5997</guid>
		<description>Per,

To store breakpoints and their conditions, use DBSTATUS and DBSTOP as shown in this documentation example from the reference page for DBSTOP:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dbstop.html#bqxd6ek-1

I just did a quick check and this can handle conditional breakpoints as well.

Note that one "gotcha" with breakpoints and conditional breakpoints is that CLEAR ALL will clear them.  Most people are a little confused by that behavior the first time they see that.  If you want to clear all the variables you have in your workspace without clearing the breakpoints, you can use DBSTATUS before CLEAR, saving the output to a temporary MAT-file, then load the MAT-file and use DBSTOP afterward.    Alternately, if you're using a sufficiently recent version of MATLAB, you can use CLEARVARS.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clearvars.html</description>
		<content:encoded><![CDATA[<p>Per,</p>
<p>To store breakpoints and their conditions, use DBSTATUS and DBSTOP as shown in this documentation example from the reference page for DBSTOP:</p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dbstop.html#bqxd6ek-1" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dbstop.html#bqxd6ek-1</a></p>
<p>I just did a quick check and this can handle conditional breakpoints as well.</p>
<p>Note that one &#8220;gotcha&#8221; with breakpoints and conditional breakpoints is that CLEAR ALL will clear them.  Most people are a little confused by that behavior the first time they see that.  If you want to clear all the variables you have in your workspace without clearing the breakpoints, you can use DBSTATUS before CLEAR, saving the output to a temporary MAT-file, then load the MAT-file and use DBSTOP afterward.    Alternately, if you&#8217;re using a sufficiently recent version of MATLAB, you can use CLEARVARS.</p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clearvars.html" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clearvars.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: per isakson</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5996</link>
		<dc:creator>per isakson</dc:creator>
		<pubDate>Wed, 19 Nov 2008 17:55:24 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5996</guid>
		<description>It is possible to have conditional printouts. Exampel: 

function    BreakDebugTest
for ii = 1 : 3
    z = ii;
end
end

Code of conditioal breakpoint at line 3:
ii==2 &#38;&#38; ( 0==fprintf(1,'Conditional: %u\n', ii ) &#38;&#38; false )

Execution BreakDebugTest outputs _one_ line:
Conditional: 2

/per</description>
		<content:encoded><![CDATA[<p>It is possible to have conditional printouts. Exampel: </p>
<p>function    BreakDebugTest<br />
for ii = 1 : 3<br />
    z = ii;<br />
end<br />
end</p>
<p>Code of conditioal breakpoint at line 3:<br />
ii==2 &amp;&amp; ( 0==fprintf(1,&#8217;Conditional: %u\n&#8217;, ii ) &amp;&amp; false )</p>
<p>Execution BreakDebugTest outputs _one_ line:<br />
Conditional: 2</p>
<p>/per</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: per isakson</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5995</link>
		<dc:creator>per isakson</dc:creator>
		<pubDate>Wed, 19 Nov 2008 17:30:26 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5995</guid>
		<description>This is a clever trick! 

The breakpoints and the conditions (code) are stored with the Matlab session (R2008a). They are not lost by closing and opening the file, but they are lost by exiting Matlab. Where are they stored? It guess it would be useful to save and load the breakpoints. I realize this is not trivial to make a robust implementation. 

With callbacks, events, etc., it is useful to get a list of called functions. I guess it would be useful to automatically set a "breakpoint with output" in the beginning of each function (and method).

dbstatus, dbstop and a little bit of regexp to keep track of the lines might make it possible to implement rough versions of save, load and set breakpoints in the beginning of functions. Have you tried that?

/ per</description>
		<content:encoded><![CDATA[<p>This is a clever trick! </p>
<p>The breakpoints and the conditions (code) are stored with the Matlab session (R2008a). They are not lost by closing and opening the file, but they are lost by exiting Matlab. Where are they stored? It guess it would be useful to save and load the breakpoints. I realize this is not trivial to make a robust implementation. </p>
<p>With callbacks, events, etc., it is useful to get a list of called functions. I guess it would be useful to automatically set a &#8220;breakpoint with output&#8221; in the beginning of each function (and method).</p>
<p>dbstatus, dbstop and a little bit of regexp to keep track of the lines might make it possible to implement rough versions of save, load and set breakpoints in the beginning of functions. Have you tried that?</p>
<p>/ per</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5990</link>
		<dc:creator>Ken</dc:creator>
		<pubDate>Mon, 17 Nov 2008 19:25:23 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5990</guid>
		<description>Hi Ken,

Conditional breakpoints in the Editor have been around since MATLAB 7 (R14).

-Ken</description>
		<content:encoded><![CDATA[<p>Hi Ken,</p>
<p>Conditional breakpoints in the Editor have been around since MATLAB 7 (R14).</p>
<p>-Ken</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kenneth Eaton</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5989</link>
		<dc:creator>Kenneth Eaton</dc:creator>
		<pubDate>Mon, 17 Nov 2008 19:06:21 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5989</guid>
		<description>There do seem to be a lot of us Kens converging on this blog. =)

I was also wondering, are conditional breakpoints new as of the most recent releases, or do they go back a ways?

-Ken (Eaton)</description>
		<content:encoded><![CDATA[<p>There do seem to be a lot of us Kens converging on this blog. =)</p>
<p>I was also wondering, are conditional breakpoints new as of the most recent releases, or do they go back a ways?</p>
<p>-Ken (Eaton)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken</title>
		<link>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5988</link>
		<dc:creator>Ken</dc:creator>
		<pubDate>Mon, 17 Nov 2008 18:41:56 +0000</pubDate>
		<guid>http://blogs.mathworks.com/desktop/2008/11/17/adding-debugging-code-without-changing-your-code/#comment-5988</guid>
		<description>Hi Ken,

I removed the "(false)" following the "This is not zero" phrase. The "false" was in reference to "zero", rather than the "not zero". This was obviously confusing, and my fault as I added it when editing the article for Ken Atwell.

Thanks for point this out Ken.

-Ken
P.S. If the "(false)" modifier didn't confuse, people, this comment referencing 3 different Kens' surely will!</description>
		<content:encoded><![CDATA[<p>Hi Ken,</p>
<p>I removed the &#8220;(false)&#8221; following the &#8220;This is not zero&#8221; phrase. The &#8220;false&#8221; was in reference to &#8220;zero&#8221;, rather than the &#8220;not zero&#8221;. This was obviously confusing, and my fault as I added it when editing the article for Ken Atwell.</p>
<p>Thanks for point this out Ken.</p>
<p>-Ken<br />
P.S. If the &#8220;(false)&#8221; modifier didn&#8217;t confuse, people, this comment referencing 3 different Kens&#8217; surely will!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
