{"id":223,"date":"2010-03-19T20:57:49","date_gmt":"2010-03-19T20:57:49","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/03\/19\/debugging-approaches\/"},"modified":"2018-01-08T16:21:37","modified_gmt":"2018-01-08T21:21:37","slug":"debugging-approaches","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/03\/19\/debugging-approaches\/","title":{"rendered":"Debugging Approaches"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are lots of posts on the <a>MATLAB newsgroup<\/a> asking questions like why do I get this error \"xxx\"?  One of the most common answers shows users how to track down the information\r\n         for themselves.\r\n      <\/p>\r\n      <p><i>Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.<\/i><\/p>\r\n      <p>This entails learning to use the MATLAB debugger.<\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Ways to Debug<\/a><\/li>\r\n         <li><a href=\"#2\">Discussion<\/a><\/li>\r\n         <li><a href=\"#3\">Ancillary Technique<\/a><\/li>\r\n         <li><a href=\"#4\">How Do You Debug?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Ways to Debug<a name=\"1\"><\/a><\/h3>\r\n   <p>There are probably a lot of ways to debug programs. I am going to mention the ones that come to me quickly.  These include:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>editing the code and removing semicolons or adding a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/keyboard.html\"><tt>keyboard<\/tt><\/a> statement at judicious locations\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/dbstop.html\">setting a breakpoint<\/a> at a particular line and stepping through code from there\r\n         <\/li>\r\n         <li>using a variant of setting a breakpoint by using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/dbstop.html\"><tt>dbstop if error<\/tt><\/a><\/li>\r\n         <li>seeing if the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/mlint.html\"><tt>mlint<\/tt> code analyser<\/a> can help (also reachable from the Tools menu)\r\n         <\/li>\r\n         <li>comparing variants of the code using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_env\/brqxeeu-104.html#bsfh8a8\">File and Folder Comparisons tool<\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/visdiff.html\"><tt>visdiff<\/tt><\/a> for command-line access\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Discussion<a name=\"2\"><\/a><\/h3>\r\n   <p>Now that MATLAB has a great debugger (whether you use the commands or use the tools provided in the MATLAB Editor), I now\r\n      longer use the technique where I edit my code.  It's too easy to not remember all the spots I edited and have them revert,\r\n      except for my bug fix, when I'm done.\r\n   <\/p>\r\n   <p>If I'm not sure where the bug is, guessing where to place a breakpoint sometimes doesn't always work well.  In those cases,\r\n      I generally set <tt>dbstop if error<\/tt>&gt; and see where things have gone wrong.  From there, I can backtrack as needed, sometimes using the technique of setting a\r\n      breakpoint at the top of the function which elicited the error.  I can then run again and step through from the top.  Sometimes\r\n      I have to backtrack to other functions on the call stack, but at least I have a point of reference by then.\r\n   <\/p>\r\n   <p>Since I often work from the editor, rather than the command-line, I automatically see the code analysis messages.  I do find\r\n      it useful sometimes to do a side-by-side comparison of two versions of a file and I generally do so from the editor Tools\r\n      menu.\r\n   <\/p>\r\n   <h3>Ancillary Technique<a name=\"3\"><\/a><\/h3>\r\n   <p>We've all heard the adage \"a picture's worth a thousand words.\"  With that in mind, instead of just looking at the code or\r\n      variable values, I frequently find myself plotting interesting variables to get a quick look.  Often I can tell from a quick\r\n      plot exactly which variable is incorrect and that then helps me narrow down where to look for the bug.\r\n   <\/p>\r\n   <h3>How Do You Debug?<a name=\"4\"><\/a><\/h3>\r\n   <p>Do you have some helpful debugging techniques to share?  I'd love to hear them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=223#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_c24f0223ec8a4d91bec1edacfba35011() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='c24f0223ec8a4d91bec1edacfba35011 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' c24f0223ec8a4d91bec1edacfba35011';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_c24f0223ec8a4d91bec1edacfba35011()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\nc24f0223ec8a4d91bec1edacfba35011 ##### SOURCE BEGIN #####\r\n%% Debugging Approaches\r\n% There are lots of posts on the\r\n% <http:\/\/ MATLAB newsgroup>\r\n% asking questions like why do I get this error \"xxx\"?  One of the most\r\n% common answers shows users how to track down the information for\r\n% themselves.\r\n%\r\n% _Give a man a fish and you feed him for a day. Teach a man to\r\n% fish and you feed him for a lifetime._\r\n%\r\n% This entails learning to use the MATLAB debugger.\r\n\r\n%% Ways to Debug\r\n% There are probably a lot of ways to debug programs. I am going to mention\r\n% the ones that come to me quickly.  These include:\r\n%\r\n% * editing the code and removing semicolons or adding a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/keyboard.html\r\n% |keyboard|> statement at judicious locations\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/dbstop.html\r\n% setting a breakpoint> at a particular line and stepping through code from\r\n% there\r\n% * using a variant of setting a breakpoint by using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/dbstop.html\r\n% |dbstop if error|> \r\n% * seeing if the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/mlint.html\r\n% |mlint| code analyser> can help (also reachable from the Tools menu)\r\n% * comparing variants of the code using the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_env\/brqxeeu-104.html#bsfh8a8 \r\n% File and Folder Comparisons tool> or\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/visdiff.html\r\n% |visdiff|> for command-line access\r\n%\r\n%% Discussion\r\n% Now that MATLAB has a great debugger (whether you use the commands or\r\n% use the tools provided in the MATLAB Editor), I now longer use the\r\n% technique where I edit my code.  It's too easy to not remember all the\r\n% spots I edited and have them revert, except for my bug fix, when I'm\r\n% done.\r\n%\r\n% If I'm not sure where the bug is, guessing where to place a breakpoint\r\n% sometimes doesn't always work well.  In those cases, I generally set\r\n% |dbstop if error|> and see where things have gone wrong.  From there, I\r\n% can backtrack as needed, sometimes using the technique of setting a\r\n% breakpoint at the top of the function which elicited the error.  I can\r\n% then run again and step through from the top.  Sometimes I have to\r\n% backtrack to other functions on the call stack, but at least I have a\r\n% point of reference by then.\r\n%\r\n% Since I often work from the editor, rather than the command-line, I\r\n% automatically see the code analysis messages.  I do find it useful\r\n% sometimes to do a side-by-side comparison of two versions of a file and I\r\n% generally do so from the editor Tools menu.\r\n%% Ancillary Technique\r\n% We've all heard the adage \"a picture's worth a thousand words.\"  With\r\n% that in mind, instead of just looking at the code or variable values, I\r\n% frequently find myself plotting interesting variables to get a quick\r\n% look.  Often I can tell from a quick plot exactly which variable is\r\n% incorrect and that then helps me narrow down where to look for the bug.\r\n%% How Do You Debug?\r\n% Do you have some helpful debugging techniques to share?  I'd love to hear\r\n% them <https:\/\/blogs.mathworks.com\/loren\/?p=223#respond here>.\r\n##### SOURCE END ##### c24f0223ec8a4d91bec1edacfba35011\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are lots of posts on the MATLAB newsgroup asking questions like why do I get this error \"xxx\"?  One of the most common answers shows users how to track down the information\r\n      ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/03\/19\/debugging-approaches\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,13],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/223"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":2626,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/223\/revisions\/2626"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}