4.04478

4.0 | 68 ratings Rate this file 91 downloads (last 30 days) File Size: 229.6 KB File ID: #2529

MATLAB Programming Style Guidelines

by Richard Johnson

 

11 Oct 2002 (Updated 21 Feb 2003)

Code covered by BSD License  

13 pages of advice on writing clear code.

Editor's Notes:

Some users have experienced problems downloading this PDF file due to a bad interaction between Adobe Reader and Internet Explorer. As a workaround, try right-clicking on the "Download this File" link on the right side of the window and use "Save Target As..." to save the file to your computer. You can then view this file directly with Adobe Reader.

This file was a File Exchange Pick of the Week

Download Now | Watch this File

File Information
Description

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

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (72)
19 Nov 2002 Matthew Simoneau

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

19 Nov 2002 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.

17 Dec 2002 daniel he  
24 Dec 2002 nandha kumar

good

01 Jan 2003 Predrag Kovacevic  
05 Jan 2003 eduardo perez  
10 Jan 2003 Tanveer Yasser  
22 Jan 2003 Lei Wang

Good convention for programming large codes

22 Jan 2003 Matlab User

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

22 Jan 2003 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.

23 Jan 2003 a l  
30 Jan 2003 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?

12 Feb 2003 Angelo Stranieri

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

24 Feb 2003 Cristian Gutiérrez

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

11 Mar 2003 vikas jain

thanks for such facility.

11 Apr 2003 John Bucsek

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

16 Apr 2003 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.

29 Apr 2003 Viet Tung NGUYEN  
12 May 2003 Alexander Toniatti  
30 Jun 2003 Adam Remy  
14 Jul 2003 James Cline

Concise, good examples

24 Jul 2003 Tom Alen  
05 Aug 2003 ramin vasfpour  
30 Aug 2003 kikaw wahyudi  
17 Sep 2003 old programmer

no news

03 Oct 2003 Dennis James

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

09 Oct 2003 hamid reza mohammadi  
15 Oct 2003 Anatoly Shadrin  
20 Oct 2003 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.

20 Nov 2003 sudharsan kannan  
10 Dec 2003 w j

good

16 Dec 2003 Bharani Krishnamurthy

very good

13 Jan 2004 Fahad Al Mahmood

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

14 Jan 2004 Michael Villalba

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

06 Feb 2004 Saeed Attarzadeh

It's great

23 Mar 2004 nhgg jhgu  
27 Mar 2004 shivanand b  
03 Apr 2004 nigus Habtu

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

05 Apr 2004 Mark Newton

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

07 Apr 2004 William Zimmerman

Concise and thorough

13 May 2004 aki arso

ok

26 May 2004 takis soteropoulos  
17 Jun 2004 Gavril Grebenisan

The file is damaged and I cannot open

22 Jun 2004 skr sleem  
30 Jun 2004 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.

26 Jul 2004 H Patel

File is damaged and I couldn't open it.

28 Jul 2004 muthu kumar

i am very greatful

14 Aug 2004 - -  
04 Sep 2004 Marcio Andre Andre

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

07 Oct 2004 James Bonanno  
09 Dec 2004 Richard Kirkeeide

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

09 Dec 2004 John Solar  
19 Dec 2004 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.

05 Jan 2005 Rene Just Nielsen  
10 Jan 2005 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)

16 Jan 2005 shafi rahman  
21 Jan 2005 schmitz koeln

na ja et jet su

01 Feb 2005 Izru Garner

Useful by all people who begin to programming.

14 Feb 2005 Yun-man Jung

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

05 Jul 2005 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.

22 Jul 2005 Mohamed Mostafa  
30 Nov 2005 Jas Ellis

Very good.

05 Dec 2005 Krishna Pillai

Nice doc.

15 Apr 2006 muhammad islam

it need more n more improvements for modern age.

06 Nov 2006 Karthik Raja T

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

03 Jan 2007 Vishnuvenkatesh Dhage

Nice and good document

19 Jun 2007 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.).

28 Jun 2007 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.

10 Apr 2008 Jeff Karas

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

10 Apr 2008 Patrick Stallings

Short, simple, straightforward, and surprisingly helpful.

29 Nov 2008 Ryan Ollos  
14 May 2009 Braden Hancock

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.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
whitepaper Richard Johnson 22 Oct 2008 06:52:42
article Richard Johnson 22 Oct 2008 06:52:42
paper Richard Johnson 22 Oct 2008 06:52:42
style Richard Johnson 22 Oct 2008 06:52:42
guidelines Richard Johnson 22 Oct 2008 06:52:42
conventions Richard Johnson 22 Oct 2008 06:52:42
documentation Richard Johnson 22 Oct 2008 06:52:42
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com