{"id":2369,"date":"2009-01-16T16:43:26","date_gmt":"2009-01-16T16:43:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/01\/16\/avoiding-coding-pitfalls\/"},"modified":"2016-11-01T17:40:08","modified_gmt":"2016-11-01T21:40:08","slug":"avoiding-coding-pitfalls","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/01\/16\/avoiding-coding-pitfalls\/","title":{"rendered":"Avoiding Coding Pitfalls"},"content":{"rendered":"<div class=\"alert alert-info\"> <span class=\"alert_icon icon-alert-info-reverse\"><\/span><p class=\"alert_heading\"><strong>Note<\/strong><\/p><p>The file submission referenced in this post is no longer available on File Exchange.<\/p><\/div>\r\n\r\nWhenever <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/9344\">Loren Shure<\/a> posts a new discussion about writing MATLAB code, anyone who wants to improve his or her technique should take notice. Loren\r\nmay be the most MATLAB-savvy person I know, and is certainly my go-to person when I'm having difficulty understanding some\r\nMATLAB concept or implementation. This week, I would like to call attention to a new submission on the File Exchange to make sure that everyone knows it's there. Although the file was submitted by fellow MathWorker <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/26588\">Linda Webb<\/a>, it is actually a reprint of a <a href=\"https:\/\/www.mathworks.com\/company\/newsletters.html?s_cid=nws_flex\">News &amp; Notes<\/a> article that Loren wrote in 2008.\r\n\r\n&nbsp;\r\n\r\nWith concise examples, the article points out common pitfalls in MATLAB concerning: matrix and element-wise multiplication,\r\ntranspose and conjugate transpose, using i or j as variables, command\/function duality, and floating-point comparisons. Loren\r\nalso provides some great tips on writing robust programs, with brief discussions of function handles, the use of \"eval.\"\r\n\r\nI encourage anyone who wants to learn from the master to download and read Loren's article. And make sure to subscribe to your favorite MathWorks newsletter, while you're at it. They're packed with pearls of wisdom!\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_453456ac06e944e1b6a2595e7192e853() {\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='453456ac06e944e1b6a2595e7192e853 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 453456ac06e944e1b6a2595e7192e853';\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 = 'Brett Shoelson';\r\n        copyright = 'Copyright 2009 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.7<\/p>\r\n\r\n<\/div>\r\n<!--\r\n453456ac06e944e1b6a2595e7192e853 ##### SOURCE BEGIN #####\r\n%% Avoiding Coding Pitfalls\r\n% Whenever\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/9344 Loren Shure> posts a new discussion about writing MATLAB\r\n% code, anyone who wants to improve his or her technique should\r\n% take notice. Loren may be the most MATLAB-savvy person I know,\r\n% and is certainly my go-to person when I'm having difficulty\r\n% understanding some MATLAB concept or implementation. This week,\r\n% I would like to call attention to a <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22597 new submission> on the File\r\n% Exchange to make sure that everyone knows it's there. Although\r\n% the file was submitted by fellow MathWorker <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/26588 Linda Webb>, it is\r\n% actually a reprint of a\r\n% <https:\/\/www.mathworks.com\/company\/newsletters.html?s_cid=nws_flex % News & Notes> article that Loren wrote in 2008.\r\n\r\n%%\r\n% With concise examples, the article points out common pitfalls\r\n% in MATLAB concerning: matrix and element-wise\r\n% multiplication, transpose and conjugate transpose, using i or j\r\n% as variables, command\/function duality, and floating-point\r\n% comparisons. Loren also provides some great tips on writing\r\n% robust programs, with brief discussions of function handles,\r\n% the use of \"eval.\"\r\n\r\n%%\r\n% I encourage anyone who wants to learn from the master to\r\n% download and read Loren's article. And make sure to <https:\/\/www.mathworks.com\/company\/newsletters.htmlsubscribe_home.html subscribe> to\r\n% your favorite MathWorks newsletter, while you're at it. They're\r\n% packed with pearls of wisdom!\r\n##### SOURCE END ##### 453456ac06e944e1b6a2595e7192e853\r\n-->","protected":false},"excerpt":{"rendered":"<p> NoteThe file submission referenced in this post is no longer available on File Exchange.\r\n\r\nWhenever Loren Shure posts a new discussion about writing MATLAB code, anyone who wants to improve his or... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/01\/16\/avoiding-coding-pitfalls\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2369"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2369"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2369\/revisions"}],"predecessor-version":[{"id":8003,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2369\/revisions\/8003"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}