File Exchange Pick of the Week

January 16th, 2009

Avoiding Coding Pitfalls

Whenever Loren Shure posts a new discussion about writing MATLAB code, anyone who wants to improve his or her technique should take notice. Loren may be the most MATLAB-savvy person I know, and is certainly my go-to person when I'm having difficulty understanding some MATLAB 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 Linda Webb, it is actually a reprint of a News & Notes article that Loren wrote in 2008.

With concise examples, the article points out common pitfalls in MATLAB concerning: matrix and element-wise multiplication, transpose and conjugate transpose, using i or j as variables, command/function duality, and floating-point comparisons. Loren also provides some great tips on writing robust programs, with brief discussions of function handles, the use of "eval."

I 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!


Get the MATLAB code

Published with MATLAB® 7.7

2 Responses to “Avoiding Coding Pitfalls”

  1. Steve L replied on :

    You gave Loren all that praise and forgot to link to her blog? :)

  2. Brett replied on :

    Mea culpa…yes, indeed I did. Thanks, Steve, for pointing out my omission!

    I certainly encourage everyone to link and subscribe to Loren’s blog, “Loren on the Art of MATLAB.” Steve’s hyperlink will take you there, or you can follow this URL:

    http://blogs.mathworks.com/loren/

    Cheers,
    Brett

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Jon: This is a useful tool - but in fairness I have to point out that it is remarkably similar to a routine of the...
  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.