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.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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