{"id":330,"date":"2012-01-13T07:21:09","date_gmt":"2012-01-13T12:21:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=330"},"modified":"2012-01-12T07:25:53","modified_gmt":"2012-01-12T12:25:53","slug":"best-practices-for-programming-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2012\/01\/13\/best-practices-for-programming-matlab\/","title":{"rendered":"Best Practices for Programming MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I thought I would share my top goto list of things I try to do when I write MATLAB code. And checking with other MathWorks\r\n         folks whose code I admire, I found they basically used the same mental list that I use.  You can find blog posts on all of\r\n         these topics by selecting relevant categories from the right side of The Art of MATLAB blog site.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">My List of Best Practices<\/a><\/li>\r\n         <li><a href=\"#2\">Missing from Your List?  Additions to My List?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>My List of Best Practices<a name=\"1\"><\/a><\/h3>\r\n   <p>Clearly (at least to me), this is not everything you generally need to do.  You still need to comment the code, add good help\r\n      information and examples, etc.  But these are the main coding practices and tools I always rely on.\r\n   <\/p>\r\n   <div>\r\n      <ol>\r\n         <li>Vectorize (but sensibly).<\/li>\r\n         <li>Use <tt>bsxfun<\/tt> in lieu of <tt>repmat<\/tt> where possible.\r\n         <\/li>\r\n         <li>When looping through an array, loop down columns to access memory in the same order that MATLAB stores the data in.<\/li>\r\n         <li>Profile the code.  I am often surprised about what is taking up the time.<\/li>\r\n         <li>Pay attention to messages from the Code Analyzer.<\/li>\r\n         <li>Use functions instead of scripts.<\/li>\r\n         <li>Don't \"poof\" variables into any workspaces. Translation, don't use <tt>load<\/tt> without a left-hand side; avoid <tt>eval<\/tt>, <tt>evalin<\/tt>, and <tt>assignin<\/tt>.\r\n         <\/li>\r\n         <li>Use logical indexing instead of <tt>find<\/tt>.\r\n         <\/li>\r\n         <li>Avoid global variables.<\/li>\r\n         <li>Don't use equality checks with floating point values.<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <h3>Missing from Your List?  Additions to My List?<a name=\"2\"><\/a><\/h3>\r\n   <p>What's on my list that you don't currently do?  Do you have a <b>major<\/b> addition to my list (there can't be too many, or I won't remember to do them all!)? Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=330#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_99ce53a4a3924039a56c831f8cd368d8() {\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='99ce53a4a3924039a56c831f8cd368d8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 99ce53a4a3924039a56c831f8cd368d8';\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 2012 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_99ce53a4a3924039a56c831f8cd368d8()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\n99ce53a4a3924039a56c831f8cd368d8 ##### SOURCE BEGIN #####\r\n%% Best Practices for Programming MATLAB\r\n% I thought I would share my top goto list of things I try to do when I\r\n% write MATLAB code. And checking with other MathWorks folks whose code I\r\n% admire, I found they basically used the same mental list that I use.  You\r\n% can find blog posts on all of these topics by selecting relevant\r\n% categories from the right side of The Art of MATLAB blog site.\r\n%% My List of Best Practices\r\n% Clearly (at least to me), this is not everything you generally need to\r\n% do.  You still need to comment the code, add good help information and\r\n% examples, etc.  But these are the main coding practices and tools I\r\n% always rely on.\r\n%\r\n% # Vectorize (but sensibly).\r\n% # Use |bsxfun| in lieu of |repmat| where possible.\r\n% # When looping through an array, loop down columns to access memory in\r\n% the same order that MATLAB stores the data in.\r\n% # Profile the code.  I am often surprised about what is taking up the\r\n% time.\r\n% # Pay attention to messages from the Code Analyzer.\r\n% # Use functions instead of scripts.\r\n% # Don't \"poof\" variables into any workspaces. Translation, don't use\r\n% |load| without a left-hand side; avoid |eval|, |evalin|, and |assignin|.\r\n% # Use logical indexing instead of |find|.\r\n% # Avoid global variables.\r\n% # Don't use equality checks with floating point values.\r\n%\r\n%% Missing from Your List?  Additions to My List?\r\n% What's on my list that you don't currently do?  Do you have a *major*\r\n% addition to my list (there can't be too many, or I won't remember to do\r\n% them all!)? Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=330#respond\r\n% here>.\r\n##### SOURCE END ##### 99ce53a4a3924039a56c831f8cd368d8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I thought I would share my top goto list of things I try to do when I write MATLAB code. And checking with other MathWorks\r\n         folks whose code I admire, I found... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2012\/01\/13\/best-practices-for-programming-matlab\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/330"}],"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=330"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":334,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/330\/revisions\/334"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}