{"id":295,"date":"2017-02-14T14:24:53","date_gmt":"2017-02-14T13:24:53","guid":{"rendered":"https:\/\/rosetta.vn\/short\/?p=295"},"modified":"2017-02-14T14:26:39","modified_gmt":"2017-02-14T13:26:39","slug":"top-10-matlab-code-practices-that-make-me-cry","status":"publish","type":"post","link":"https:\/\/rosetta.vn\/short\/2017\/02\/14\/top-10-matlab-code-practices-that-make-me-cry\/","title":{"rendered":"Top 10 MATLAB code practices that make me cry"},"content":{"rendered":"<header>Here is a short list of bad coding habits with Matlab (and also Octave), written by Doug Hull in 2010. A more complete book to learn about coding (computer programming) style is &#8220;Clean Code&#8221; by Robert C. Martin.<\/header>\n<blockquote>\n<header>Posted by <b class=\"h-entry h-name\">Doug Hull<\/b>, March 8, 2010<\/header>\n<div class=\"content_body\">\n<p>I was chatting with the Application Support Engineers here at MathWorks about what kind of coding practices cause avoidable pain for MATLAB users.Without further ado: The top ten and quick ways to not do them:<\/p>\n<p><strong>10.) Not using left hand zeros<\/strong><\/p>\n<p>Certain things must be learned the hard way. I learned this one bleary eyed evening as an undergraduate. Well into the night working on a MATLAB homework assignment that would \u201ctake ten minutes, fifteen if you type slow.\u201d (yes, Dr. P, 13 years later I still remember that one! -smile-)<\/p>\n<p>It is really easy to mistake a <em>.5<\/em> for a <em>5<\/em>. That is why I always use a left hand zero like <em>0.5<\/em>.<\/p>\n<p><strong>9.) Plotting enormous amounts of data<\/strong><\/p>\n<p>My computer monitor has 2.3 million pixels, total. If I try to plot datasets with huge amounts of data in them, they will very often just look like a blob and slow the machine down in the process.<\/p>\n<p>There is very often a better visualization available. Here is <a href=\"http:\/\/blogs.mathworks.com\/videos\/2010\/01\/22\/advanced-making-a-2d-or-3d-histogram-to-visualize-data-density\/\">an example<\/a> of changing the visualization to make it clearer and less taxing on memory.<\/p>\n<p><strong>8.) GUIs with garish color schemes<\/strong><\/p>\n<p>In an effort to emphasize certain buttons on their GUI, people will change the colors of them. Very quickly they end up with several different colored buttons, a non-standard background color, extra big buttons, etc\u2026<\/p>\n<p>Sticking with the default colors is a good move. Most professionally produced software sticks with the defaults, it ends up looking better.<\/p>\n<p><strong>7.) Using <em>ans<\/em>, or any other MATLAB function as a variable name or function.<\/strong><\/p>\n<p>When you do this, MATLAB will call whichever one is higher on the path. Some strange behavior can occur when you redefine a function like that. Unfortunately, MATLAB does not catch you doing this for the most part.<\/p>\n<p>I try to avoid using variables and function names that are common terms like, <em>mean<\/em>, <em>filter<\/em>, etc\u2026 If there is any doubt, use the <em>which<\/em> command to find out if a function exists of a given name.<\/p>\n<p><strong>6.) Not using white space to good effect in code.<\/strong><\/p>\n<p>Even though you can put several commands on one line if separated by a semicolon, these lines can often be hard to notice. Not putting blank lines between sections of code can also make it harder to read.<\/p>\n<p>White space is free, use it to make your code look good.<\/p>\n<p><strong>5.) Bad variable names<\/strong><\/p>\n<p>Variable names are often the only commenting that gets added to people\u2019s code. Meaningful variable names are a great opportunity to make the meaning of your code more clear and to some degree, self-documenting.<\/p>\n<p>Avoid using variable names like <em>temp<\/em>, <em>aaa<\/em>, <em>r247899921<\/em>. These just do not convey as much information to people that have to read your code as<em>flagPassedInspection<\/em>, <em>centroidX<\/em>, <em>fidCurrentFile<\/em>.<\/p>\n<p><strong>4.) Hard coding data into the MATLAB code file<\/strong><\/p>\n<p>Some people like to put some of their variables directly into the MATLAB code. That makes sense for <em>small<\/em> variables (I will let you define what small means for you). For instance, I would feel fine putting a 3\u00d73 matrix into my code. I would think twice about a 10\u00d710, and I would start using one of our file readers for a 100 x 100.<\/p>\n<p>The worst instance I ever saw of this was some MATLAB code where the .M file was 4 GIG (not a mistake) long. All but a small amount of that was data written out in ASCII. This makes your code hard to read, maintain and understand.<\/p>\n<p><strong>3.) Exceptionally long files<\/strong><\/p>\n<p>Even if not hard coding data into a MATLAB code file, it is easy to just add on \u201cjust a few more lines of code\u201d until you have thousands of lines of code in a single script. This makes your code hard to understand.<\/p>\n<p>I <em>try<\/em> to use the rule that I should be able to see an entire script or function in one screen. This is not entirely practical, so I will at least break the code into logical sections that do fit on screen all at once.<\/p>\n<p><strong>2.) Globals<\/strong><\/p>\n<p>I have never seen MATLAB code where globals were the right thing to do. Exception: functions TIC and TOC use them quite nicely. Most of the time I have seen globals being used it was a situation where the code author did not understand scoping of variables. Rather than pass variables from one function to another, they were just being made global.<\/p>\n<p>Why are people cautioned against using global variables? I will leave that to the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Global_variable\">consensus on Wikipedia<\/a>.<\/p>\n<p><strong>1.) Eval<\/strong><\/p>\n<p>EVAL is right up there with globals. MATLAB user will often string together MATLAB commands to get sequential variable names <em>s1<\/em>, <em>s2<\/em>, <em>s3<\/em>\u2026 only to then have to use another EVAL statement to work with the sequential variable names! Very often, a cell array indexed with s{1}, s{2}, s{3}\u2026 would work much better.<\/p>\n<p>I will also find that people use EVAL to get at certain fields in a structure (for example data.alpha) when they do not know at the time of writing the code what field they will want. Now the <a href=\"http:\/\/blogs.mathworks.com\/videos\/2009\/02\/27\/dynamic-field-name-usage\/\">\u201c.parens\u201d notation<\/a> makes that easier.<\/p>\n<p>The other most common place to see people use EVAL when it is not needed is when they are trying to load a file or some other function like that. Very often they are trying to EVAL a string like \u201cload filename.mat\u201d not realizing that there is a functional form where you can use fileNameString = \u2018filename.mat\u2019; load(fileNameString)<\/p>\n<p>Follow <a href=\"https:\/\/twitter.com\/stuartmcgarrity\">@stuartmcgarrity<\/a> on Twitter to be notified of new posts.<\/p>\n<\/div>\n<\/blockquote>\n<p>Source: <em><a href=\"https:\/\/blogs.mathworks.com\/videos\/2010\/03\/08\/top-10-matlab-code-practices-that-make-me-cry\/\">Top 10 MATLAB code practices that make me cry \u00bb Stuart\u2019s MATLAB Videos<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a short list of bad coding habits with Matlab (and also Octave), written by Doug Hull in 2010. A more complete book to learn about coding (computer programming) style is &#8220;Clean Code&#8221; by Robert C. Martin. Posted by<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[30,215],"tags":[221,219,220,47],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p8jhJx-4L","_links":{"self":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts\/295"}],"collection":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":0,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"wp:attachment":[{"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rosetta.vn\/short\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}