Guidelines for MATLAB Code Writing


Most of us engineers will surely use MATLAB to write code for simulation or complex calculation. The problem is everybody write code with their own style and most of them write badly. What I mean by ‘bad’ here is not that the code’s algorithm are bad or that the result of code is inaccurate. What I mean by ‘bad’ here is their code writing.

Few days back, I was checking out MATLAB’s facebook page and they posted a guideline for good MATLAB programming. It is a good guidelines and I think everyone who uses MATLAB or wanted to learn MATLAB should read these guidelines first. The guideline talks about how to write a good code, that is readable, understandable, shareable, easy to debug, and easy to modify. Me myself has followed several points stated in the guideline but unfortunately there are several point that I haven’t implemented yet. You can download the Guideline for MATLAB Code Writing here. It’s free.:D

Now, for this posting, I’m going to point out several important points stated in the guideline that I think is really important to be followed. Here goes:

Name your variable well. Yup, when you are writing a code and assigning a variable, name it with a descriptive name. Dont use short unclear variable such as a, b, c. For example, if you are assigning air density value, use density=1.225. It will make your code easier to understand. If you use unclear variable such as b=7, it will be unclear what is b.

Preserve variable i, j, k, m, n for iterator notation. Yup, this is common convention. I found it on all tutorial and all code tht I’ve read.

Comment a lot. Use % to initiate a comment line (for those who don’t know, comment line is a line what will not be processed/compiled.) and explain what will you do in each section of your comment. Here is a good example of comment use:

%Declaring variables

density=1.225;

airspeed=100;

%Reading airfoil data

load airfoil.mat

%Calculating lift

Lift=CalculateLift(af,density,airspeed)

%Calculating drag coef

Drag=CalculateDrag(af,density,airspeed)

%Plotting drag polar

plot (Drag,Lift)

axis equal

Use indentation. Indentation is a good way to provide code a structure. It allows you to see whether a statement is done inside a loop or conditional, etc. With this, it is your code will be much more well shape and more understandable. So, whenever your are writing a loop or conditional statement, be sure to give indentation to these statements.

Split your statement. MATLAB editor gives you a line at the right side to mark that it is the 80th column. If one of your statement is very long, you may passed this line. It wont do you any wrong to pass this line. However, it is better to split your statement and continue it at the next row using three dots (…). The purpose of splitting the statement is for when you are copying your code to other text processor, like word, it will look nicer with your statement splitted. This is an example how to split a statement

Lift=0.5*density*(airspeed^2)*…

         CalculateCL(airfoil,Re,airspeed);

Make documentation for your function m-files. Yup, in function m-files, an input is needed for the function to run and then the m-files will provide something as an output. Since you are the only one that make the code, you’ll the only one that will know the input and output for the m-file. So, write a documentation about how to use the function, so tht everyone else can use it. Documentation is usually written with %% and is placed after function declarer and before the first statement of the code. With documentation on your code, pepole can use help command in MATLAB to see how your function works. Just like what it is said at the guidelines : “Documentation is like sex. When it’s good, it is very very good. But when it’s bad, it is still better than nothing”

Well, I think that’s all the important point in writing a good MATLAB code. If you want to know more, you can download the guideline from the link above. Hope you find this useful.

Regards,

0X_R

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Guidelines for MATLAB Code Writing

  1. Junjunan says:

    “Documentation is like sex. When it’s good, it is very very good. But when it’s bad, it is still better than nothing” Like this lhaa pen.. :))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s