File Exchange

image thumbnail

Guidelines for writing clean and fast code in MATLAB

version 1.8 (390 KB) by

This document gives hints on how to write fast and good quality MATLAB code.

53 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This document is aimed at all MATLAB users who are already familiar with the syntax but may not yet know the each and every pitfall of MATLAB programming. Its goal is to give a number of hints which enable the reader to write quality MATLAB programs and to avoid commonly made mistakes.
There are three major independent chapters which may very well be read separately. Also, the individual chapters each split up into one or two handful of chunks of information. In that sense, this document is really a slightly extended list of dos and don’ts.
This guide is written as part of a basic course in numerical analysis, most examples and codes will hence tend to refer to numerical integration or differential equations. However, almost all aspects are of general nature and will also be of interest to anyone using MATLAB.
Feel free to contribute at https://github.com/nschloe/matlab-guidelines.

Comments and Ratings (14)

YEONHWA KIM

E. Cheynet

Adam Danz

Adam Danz (view profile)

Generally very sound advice, with concrete illustrations in code.

Personally I prefer not to include helper functions within other functions, unless the helper function is a short "anonymous function":
http://au.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html?s_tid=doc_12b

One other tip: besides building "fences", now in MATLAB you can identify "code sections" by use of two consecutive percent symbols (%%), which acts something like a 'super comment'. When opened in MATLAB, the code sections are visually distinct, and can easily be run separately in a 'procedural' script (less easily in a function).
http://au.mathworks.com/help/matlab/matlab_prog/run-sections-of-programs.html?searchHighlight=section%20comment

Praviraj PG

Praviraj PG (view profile)

Nico Schlömer

Nico Schlömer (view profile)

@Mark Mikofski Definitely a good idea to add this.

Mark Mikofski

Mark Mikofski (view profile)

Julia (http://www.julialang.com) is a new language that has the potential to be an alternative to MATLAB. It is targeted at MATLAB users, but it's speed approaches native C, is open-source (as in free beer) and has parallelism built in. Python has obviously been around longer, but Julia out performs Python by a order of magnitude.

Paul Metcalf

Michael Völker

Vaclav Bouse

nice, clear, overview, worthy to read

Roger Parkyn

Good clearly presented examples make this easy to read but still informative. Will be useful for people new to Matlab (and I learnt a few things from it too!).

Updates

1.8

small style updates

1.7

typos, small updates

1.6

* added section on Python
* fixed some typos

1.3

typo in description

1.2

- changed colors
- added paragraph on logical indexing
- updated license

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video