File Exchange

image thumbnail

MATLAB Programming Style Guidelines

version 1.0 (230 KB) by

13 pages of advice on writing clear code.

4.17722
80 Ratings

49 Downloads

Updated

View License

Editor's Note: This file was a File Exchange Pick of the Week

Advice on writing MATLAB code usually addresses efficiency concerns, with recommendations such as "Don't use loops." This document is different. Its concerns are correctness, clarity and generality. The goal of these guidelines is to help produce code that is more likely to be correct, understandable, sharable and maintainable.

This document lists MATLAB coding recommendations consistent with best practices in the software development community. These guidelines are generally the same as those for C, C++ and Java, with modifications for MATLAB features and history. The recommendations are based on guidelines for other languages collected from a number of sources and on personal experience.

This file is also available at http://www.datatool.com/prod02.htm

Comments and Ratings (87)

Tom Hawkins

Good work!

Richard Johnson

Richard Johnson (view profile)

I have just submitted an updated and revised version named "MATLAB Style Guidelines 2.0". It is file ID 46056. Thanks for all the helpful feedback.

Bryce

Bryce (view profile)

Nice paper. Is there any chance that an MS Word version could be made available so that I can make small updates that are specific to my organization? But if that violates the copyright then I understand.

Excellent work! I agree with the comment that the use of i and j of variables should be avoided altogether (because i and j are used to work with complex numbers) and to start iterators with k. But that's the only fault I find. I've been using a lot of this for years, but there's still some useful new insights here as well.

Hugh

Hugh (view profile)

Excellent guidelines.

The 14 pages describe practices I've already used for years, but remind me well why to apply them.

In addition, the citations add some fun while reading it.

Thanks a lot!

Chandra Evans

Quite! This was a very great publish. Thanks for the supplied info. You can read about Programming Style at http://www.bobyhermez.com/2011/06/programming-style-part-1/

Eric Johnson

Eric Johnson (view profile)

Richard Johnson

Richard Johnson (view profile)

I now have a book on this topic.
<a
href="http://www.amazon.com/gp/product/0521732581?ie=UTF8&tag=datatoolcom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0521732581">The
Elements of MATLAB Style</a><img
src="http://www.assoc-amazon.com/e/ir?t=datatoolcom-20&l=as2&o=1&a=0521732581"
alt=""
style="border: medium none ! important; margin: 0px ! important;"
border="0" height="1" width="1">
It has been reviewed by Loren Shure in her blog.
<a
href="http://blogs.mathworks.com/loren/2011/02/10/book-review-the-elements-of-matlab-style/">Loren's
review
</a>

Paul

Paul (view profile)

I'd like to recommend starting a wikibook on this topic.

M Koelemay

Mathijs

This is great to have! I'm glad I finally have some sort of reference on what are standard accepted practices in more than just my own circle of users.

Ryan Ollos

Patrick Stallings

Short, simple, straightforward, and surprisingly helpful.

Jeff Karas

Good food for thought. I like the sense of humor, particularly "% 24 November 1971, D.B. Cooper, exit conditions modified."

Asaf Horesh

I read several gudilines documents, this one was far better than the others.
comment: the use of Capitals in function names is wide spread, and much more clarifying for the meaning.

oliv -

Several mistakes in that document:
1-The use of underscore is said to be uncommon in other languages, which is a very partial assertion. It is of common use in C, which is quite a common language. This document shows how to write unreadable variable names by not using the underscore.
2- the use of i and j is mentioned for scratch variables. 'i' and 'j' are matlab constants (sqaure root of -1), and redefining 'i' is one of the hardest bug to debug (unless you are well aware of that possibility). The good Matlab programmer should avoid using 'i' (or 'j').
3- Use of uppercase letters in function names is perfectly valid, contrary to what the document suggests. There are cases where X and x have different meanings. Think about colorimetry, where x,y,z are the chromaticity coordinates and X,Y,Z the tristimuli. Matlab offers the freedom of using both uppercase and lowercase, it should be used reasonably (e.g. for an acronym, a name, etc.).

Vishnuvenkatesh Dhage

Nice and good document

Karthik Raja T

Good, I will be thankful if u send me any more guidelines materials like complete soft copies..!

muhammad islam

it need more n more improvements for modern age.

Krishna Pillai

Nice doc.

Jas Ellis

Very good.

Mohamed Mostafa

I Am a Loser Loser

Well, the author says: "Common scratch variables for integers are i, j, k, m, n and for doubles x, y and z." However, we should avoid using i and j as variable names because they are by default the unit of imagenary numbers. To be more precise, in Matlab, i^2 == j^2 == -1. Using i and j as varaible names may cause errors hard to detect.

Yun-man Jung

Thanks a lot. So much useful reference for
novice then.

Izru Garner

Useful by all people who begin to programming.

schmitz koeln

na ja et jet su

shafi rahman

Frederic Bosche

Really helpful. everybody should read something like this before starting any coding ! (Now, it;s my turn to review everything that I did)

Rene Just Nielsen

Fu-Sung Wang

After I read this article, I'm impressed some good stuffs that I uesed to ignore or misuderstand. This pdf file is easy understood and it's especially useful for MATLAB experienced programmers. The guidelines helped me to construct a more standardized, easy-maintained programs. No matter what kind of languages you are using, there will be some concepts you can refer to.

John Solar

Richard Kirkeeide

Clear, concise and reasonable. Should be alot of help to me (and others). Thanks

James Bonanno

Marcio Andre Andre

  Gostei muito me ajuda guando trabalho com matrizes ele é otimo se duvidas...
 MARCIO ANDRE /// FLORIANÓPOLIS SC
                BRASIL

- -

muthu kumar

i am very greatful

H Patel

File is damaged and I couldn't open it.

Fa Esu

Your documemet is not structured enough yet. Try use a standard LaTex. More examples could be helpful and thus self-explanatory. Thanks, btw.

skr sleem

Gavril Grebenisan

The file is damaged and I cannot open

takis soteropoulos

aki arso

ok

William Zimmerman

Concise and thorough

Mark Newton

Useful, short document that reminds one of all the things they should already do, but probably don't.

nigus Habtu

I am greatful to Matlab& I am very happy if give me this documment.

shivanand b

nhgg jhgu

Saeed Attarzadeh

It's great

Michael Villalba

An excellent collection, great for beginning or intermediate Matlab coders.

Fahad Al Mahmood

Great & Appreciated Guidelines!
I hope I can follow all of them!

Bharani Krishnamurthy

very good

w j

good

sudharsan kannan

John Solar

Very good. But a little too kind. Mandate instead of suggest! Also, i and j should never be used. Code may get expanded to use complex numbers. It is also a bad habit that might allow a slip in the future. You are not doing favors by allowing it sometimes.

Anatoly Shadrin

hamid reza mohammadi

Dennis James

Standards are always welcome in structured programming. This is an attempt at standardizing.

old programmer

no news

kikaw wahyudi

ramin vasfpour

Tom Alen

James Cline

Concise, good examples

Adam Remy

Alexander Toniatti

Viet Tung NGUYEN

Dr K. Olp

Generally, most of this should already be known and adhered to by any professional programmer, however it is sometimes useful to have it 'written down' as a reference. Useful as a beginners guide.

John Bucsek

No problems viewing file. A good collection of programming tips.

vikas jain

thanks for such facility.

Cristian Gutiérrez

Very instructive this material. However, requires knowing about Matlab programming.

Angelo Stranieri

Cannot download PDF. Acrobat 5.0 says there is an error in the file.

another user

Opinionated, but well thought out for issues specific to Matlab :-). I liked it! I wonder if something like this exists for Simulink as well?

a l

B G

Very helpful. I typically write in MATLAB, and I haven't read about style guidelines before. This will help me write more readable and more maintainable MATLAB code.

Matlab User

Most parts are borrowed from other languages. Can't see much useful information.

Lei Wang

Good convention for programming large codes

Tanveer Yasser

eduardo perez

Predrag Kovacevic

nandha kumar

good

daniel he

Penny Anderson

I thought this document encapsulated a boat load of good advice relevant for many programming languages, and included some nice MATLAB specific hints.

Detailed comments:
Longer, more descriptive variable, function and file names are good, except when math training takes over. I am never going to refer to A, x and b in the system of linear equations A*x=b by any other names ;-)

My exception to the rule about iterators being in alphabetical order is when using them to access entries in an array (granted, we try to discourage this behavior in the first place, but it's not always feasible). In this case, I generally use i for rows, j for columns and k for pages if it's a 3D array, regardless of the loop order. For more dimensions I would use something like i1,i2,i3, ...

In the section on Conditionals, I would replace "Complex" with "Complicated" to avoid confusion with MATLAB's complex numbers.

Matthew Simoneau

Great work. The document is well-written and spot-on. A much-needed addition to the MATLAB community.

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video