{"id":117,"date":"2007-12-07T15:31:25","date_gmt":"2007-12-07T20:31:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/07\/useful-debugging-commands-and-tips\/"},"modified":"2016-08-02T13:23:04","modified_gmt":"2016-08-02T18:23:04","slug":"useful-debugging-commands-and-tips","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/07\/useful-debugging-commands-and-tips\/","title":{"rendered":"Useful Debugging Commands and Tips"},"content":{"rendered":"<div class=\"content\">\r\n\r\nIn my personal history using computers, I have learned to embrace debuggers to help me understand what's going on in my code.\r\n\r\n&nbsp;\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n \t<li><a href=\"#1\">Short List of Lesser Known Debugging Commands<\/a><\/li>\r\n \t<li><a href=\"#2\">An Example<\/a><\/li>\r\n \t<li><a href=\"#3\">First Experiment<\/a><\/li>\r\n \t<li><a href=\"#7\">Stopping for a Caught Error<\/a><\/li>\r\n \t<li><a href=\"#9\">Common Issues<\/a><\/li>\r\n \t<li><a href=\"#10\">Handy Tip<\/a><\/li>\r\n \t<li><a href=\"#13\">Other Debugging Gotchas and Tips?<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Short List of Lesser Known Debugging Commands<a name=\"1\"><\/a><\/h3>\r\nHere's my short list of some useful debugging commands that might be less well known, especially for those of you who primarily use the interactive debugger as part of the MATLAB\r\neditor:\r\n<div>\r\n<ul>\r\n \t<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbstop.html\"><tt>dbstop if error<\/tt><\/a><\/li>\r\n \t<li><tt>dbstop if caught error<\/tt><\/li>\r\n \t<li><tt>dbstop if warning<\/tt><\/li>\r\n \t<li>variants with specific message ID<\/li>\r\n \t<li><tt>dbstop if naninf<\/tt><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>An Example<a name=\"2\"><\/a><\/h3>\r\nLet's set up an example so you can see what's going on. I first make sure my debugger state is all cleared up and then create\r\na <tt>struct<\/tt> which I will then test to see if a particular field exists.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dbclear <span style=\"color: #a020f0;\">all<\/span>\r\ns.fred = 1;\r\ndbtype <span style=\"color: #a020f0;\">isfieldTryE<\/span><\/pre>\r\n<pre style=\"font-style: oblique;\">1     function tf = isfieldTryE(s,f)\r\n2     %ISFIELD True if field is in structure array.\r\n3     %   F = ISFIELDTRYE(S,'field') returns true \r\n4     %   if 'field' is the name of a field\r\n5     %   in the structure array S.\r\n6     %\r\n7     %   See also GETFIELD, SETFIELD, FIELDNAMES.\r\n8     \r\n9     tf = true;\r\n10    try  \r\n11      tmp = s.(f);\r\n12    catch E\r\n13        % I don't care why it failed.\r\n14      tf = false;\r\n15    end\r\n16    \r\n17    \r\n\r\n<\/pre>\r\n<h3>First Experiment<a name=\"3\"><\/a><\/h3>\r\nWhat happens if I ask to see if a particular field exists, and ask for MATLAB to stop if there's an error?\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dbstop <span style=\"color: #a020f0;\">if<\/span> <span style=\"color: #a020f0;\">error<\/span>\r\nisfieldTryE(s,<span style=\"color: #a020f0;\">'fred'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n     1\r\n<\/pre>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">isfieldTryE(s,<span style=\"color: #a020f0;\">'freddy'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n     0\r\n<\/pre>\r\nWhat you can see is that testing for either an existing or non-existent field sails on through.\r\n\r\nAnd I get the same behavior if I try to stop only for an appropriate message identifier.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dbstop <span style=\"color: #a020f0;\">if<\/span> <span style=\"color: #a020f0;\">error<\/span> <span style=\"color: #a020f0;\">MATLAB:nonExistentField<\/span>\r\nisfieldTryE(s,<span style=\"color: #a020f0;\">'fred'<\/span>)\r\nisfieldTryE(s,<span style=\"color: #a020f0;\">'freddy'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n     1\r\nans =\r\n     0\r\n<\/pre>\r\n<h3>Stopping for a Caught Error<a name=\"7\"><\/a><\/h3>\r\nTo stop for an error that is <tt>caught<\/tt>, I use the following form of <tt>dbstop<\/tt> (in this case with the message identifier).\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dbstop <span style=\"color: #a020f0;\">if<\/span> <span style=\"color: #a020f0;\">caught<\/span> <span style=\"color: #a020f0;\">error<\/span> <span style=\"color: #a020f0;\">MATLAB:nonExistentField<\/span>\r\nisfieldTryE(s,<span style=\"color: #a020f0;\">'fred'<\/span>)\r\nisfieldTryE(s,<span style=\"color: #a020f0;\">'freddy'<\/span>)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n     1\r\n\r\nCaught-error breakpoint was hit in isfieldTryE at line 11. The error was:\r\n\r\nReference to non-existent field 'freddy'.\r\n\r\nans =\r\n     0\r\n<\/pre>\r\nNow when I ask for a non-existent field, the debugger stops in the <tt>catch<\/tt> block. And to resume, I use\r\n<pre>  dbcont<\/pre>\r\n<h3>Common Issues<a name=\"9\"><\/a><\/h3>\r\nThere are a few common issues that puzzle people when debugging. They include:\r\n<div>\r\n<ul>\r\n \t<li>breakpoints disappearing when debugging. This is often caused by a <tt>clear all<\/tt> in code being run. See, for example, <a href=\"\">this post<\/a>.<\/li>\r\n \t<li>name collisions. These are caused either by using a variable name the same as a name for a MATLAB function, or having multiple\r\nfunctions with the same name. Using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html\"><tt>which<\/tt><\/a> can help you figure out what's going on if you realize there's a nameclash.<\/li>\r\n \t<li>variables not being present. This is often an issue of not understanding scoping in MATLAB, including inputs and outputs of functions, what workspace graphics callbacks execute in, scripts versus functions.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Handy Tip<a name=\"10\"><\/a><\/h3>\r\nHere's a useful tip I use, for debugging, or sometimes to just find out the signature of a function. Using\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbtype.html\"><tt>dbtype<\/tt><\/a> 1 <i>fileName<\/i>\r\n\r\nand you will see the first line of the file displayed. If you write your code as we tend to do at The MathWorks, the first\r\nline of a file containing functions will be the function definition line. Voila, you have the signature!\r\n<h3>Other Debugging Gotchas and Tips?<a name=\"13\"><\/a><\/h3>\r\nYou might find some additional debugging tips on <a href=\"https:\/\/blogs.mathworks.com\/pick\/\">Doug's blog<\/a>.\r\n\r\nSome people like the <tt>keyboard<\/tt> command or omitting terminating semicolons but I prefer to debug without editing my code, when possible.\r\n\r\nDo you have other tips to share that you find helpful? Post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=117#respond\">here<\/a>.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_9f8e73f04e4f424b9f07773933ac020d() {\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='9f8e73f04e4f424b9f07773933ac020d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9f8e73f04e4f424b9f07773933ac020d';\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 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<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>\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.5<\/p>\r\n\r\n<\/div>\r\n<!--\r\n9f8e73f04e4f424b9f07773933ac020d ##### SOURCE BEGIN #####\r\n%% Useful Debugging Commands and Tips\r\n% In my personal history using computers, I have learned to embrace\r\n% debuggers to help me understand what's going on in my code.\r\n%% Short List of Lesser Known Debugging Commands\r\n% Here's my short list of\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f10-60570.html some useful debugging commands>\r\n% that might be less well known, especially for those of you who primarily\r\n% use the interactive debugger as part of the MATLAB editor:\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbstop.html |dbstop if error|>\r\n% * |dbstop if caught error|\r\n% * |dbstop if warning|\r\n% * variants with specific message ID\r\n% * |dbstop if naninf|\r\n%\r\n%% An Example\r\n% Let's set up an example so you can see what's going on.  I first make\r\n% sure my debugger state is all cleared up and then create a |struct| which\r\n% I will then test to see if a particular field exists.\r\ndbclear all\r\ns.fred = 1;\r\ndbtype isfieldTryE\r\n%% First Experiment\r\n% What happens if I ask to see if a particular field exists, and ask for\r\n% MATLAB to stop if there's an error?\r\ndbstop if error\r\nisfieldTryE(s,'fred')\r\n%%\r\nisfieldTryE(s,'freddy')\r\n%%\r\n% What you can see is that testing for either an existing or non-existent\r\n% field sails on through.\r\n%%\r\n% And I get the same behavior if I try to stop only for an appropriate\r\n% message identifier.\r\ndbstop if error MATLAB:nonExistentField\r\nisfieldTryE(s,'fred')\r\nisfieldTryE(s,'freddy')\r\n%% Stopping for a Caught Error\r\n% To stop for an error that is |caught|, I use the following form of\r\n% |dbstop| (in this case with the message identifier).\r\ndbstop if caught error MATLAB:nonExistentField\r\nisfieldTryE(s,'fred')\r\nisfieldTryE(s,'freddy')\r\n%%\r\n% Now when I ask for a non-existent field, the debugger stops in the\r\n% |catch| block. And to resume, I use\r\n%\r\n%    dbcont\r\n%\r\n%% Common Issues\r\n% There are a few common issues that puzzle people when debugging.  They\r\n% include:\r\n%\r\n% * breakpoints disappearing when debugging.  This is often caused by a\r\n% |clear all| in code being run.  See, for example,\r\n% < this post>.\r\n% * name collisions.  These are caused either by using a variable name the\r\n% same as a name for a MATLAB function, or having multiple functions with\r\n% the same name. Using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html |which|>\r\n% can help you figure out what's going on if you realize there's a\r\n% nameclash.\r\n% * variables not being present.  This is often an issue of not\r\n% understanding <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f7-38085.html scoping>\r\n% in MATLAB, including inputs and outputs of\r\n% functions, what workspace graphics callbacks execute in, scripts versus\r\n% functions.\r\n%\r\n%% Handy Tip\r\n% Here's a useful tip I use, for debugging, or sometimes to just find out\r\n% the signature of a function.  Using\r\n%%\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbtype.html |dbtype|> 1 _fileName_\r\n%%\r\n% and you will see the first line of the file displayed.  If you write your\r\n% code as we tend to do at The MathWorks, the first line of a file\r\n% containing functions will be the function definition line.  Voila, you\r\n% have the signature!\r\n%% Other Debugging Gotchas and Tips?\r\n% You might find some additional debugging tips on\r\n% <https:\/\/blogs.mathworks.com\/pick\/ Doug's blog>.\r\n%%\r\n% Some people like the\r\n% |keyboard| command or omitting terminating semicolons but I prefer to\r\n% debug without editing my code, when possible.\r\n%%\r\n% Do you have other tips\r\n% to share that you find helpful?  Post them\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=117#respond here>.\r\n\r\n##### SOURCE END ##### 9f8e73f04e4f424b9f07773933ac020d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nIn my personal history using computers, I have learned to embrace debuggers to help me understand what's going on in my code.\r\n\r\n&nbsp;\r\nContents\r\n\r\n\r\n \tShort List of Lesser Known Debugging... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/07\/useful-debugging-commands-and-tips\/\">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\/117"}],"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=117"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":1888,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/117\/revisions\/1888"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}