File Exchange Pick of the Week

Our best user submissions

Good MATLAB Coding Practices

Brett‘s Pick this week has three parts, and is really a tip of the hat to three authors who have taken the initiative to try to
promote and teach good MATLAB programming practices. In particular, I’d like to acknowledge Michael Robbins for “Good MATLAB Programming Practices”; Pascal Getreuer for “Writing Fast MATLAB Code”; and Nico Schlömer for “Guidelines for writing clean and fast code in MATLAB.”

I recall fondly my first attempts at writing a bit of MATLAB code; I believe the year was 1996. I fumbled around, read a few
“Getting Started” documents, and began writing code. I was amazed at how easily I could get MATLAB to do what I wanted it to do! MATLAB is
a fourth-generation interpreted language, and it’s relatively forgiving; somehow even ugly code manages to work. Over the
years, as I grew more confident (and competent) with the language, I learned just how poor my coding practices were. And in
some cases, the difference between good code and bad code can be orders of magnitude difference in performance (as measured
by computation time). It behooves the user to spend some time building a greater mastery of the language; dividends will come
not only in improved readability and maintainability, but in improved performance as well. Moments before writing this post,
I had the very unpleasant experience of looking back at my 15-year-old code; it could appear in a textbook of poor coding
practices!

Fortunately, Michael provided two versions of his “Good Programming Practices” (2003); I had a font problem with one, but
was able to read the second without difficulty. Michael’s document provides a solid page of useful gems. Among them, he advises
that: “creative use of FIND, PROD, SUM, CUMSUM, NaN, REPMAT, RESHAPE, ONES and ZEROS can really help vectorize your code,
but try not to make the code too cryptic. There is a tradeoff between execution time and readability. If your statement is
cryptic, either include an equivalent (but slow and easy to read) version in a comment or write a MEX file.” He also suggests
that the user “use PROFILE to identify where you need to work on speed improvements.”

Pascal’s “Writing Fast MATLAB Code” (2009) is more ambitious. In a beautifully formatted document, he lays out 12 chapters
on the topic of better utilization of the MATLAB language:

  • Introduction
  • The Profiler
  • Array Preallocation
  • JIT Acceleration
  • Vectorization
  • Inlining Simple Functions
  • Referencing Operations
  • Solving Ax = b
  • Numerical Integration
  • Signal Processing
  • Miscellaneous
  • Further Reading

Nico cites Pascal’s file as an inspiration for his 2009 “Guidelines.” In 34 pages, he spouts some great suggestions “aimed
at MATLAB beginners who already know the syntax but feel are not yet quite experienced with it. Its goal is to give a number
of hints which enable the reader to write quality MATLAB programs and to avoid commonly made mistakes.”

All of these files are worthy of a download and careful study. I wish I had had them back when I was getting started. It’s
easier to learn good habits than to break bad ones!

By the way, we now have a video version of “Getting Started” with MATLAB, as well as several additional MathWorks’ resources to guide the user to better coding practices.
One I particularly recommend is the recorded webinar entitled “Speeding Up MATLAB Applications.” I also recommend that the motivated reader spend a bit of time following the links in this doc page on code performance. You’ll quickly learn how to time and profile your code, plus lots of techniques for improving performance.

Comments?

Published with MATLAB® 7.11

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.