Thoughts on the Engineering Industry

A blog covering engineering, technology and business topics

Basic Tips for Programming in Matlab

Hello everyone.  I meant to have this ready for last weekend but I got a bit of a cold the week before and had two exams this week…what can I say, life throws us curve balls sometimes.  However, I am back in rhythm now.  Today, I want to talk about some basic tips to program well in Matlab.  Matlab is heavy duty math program that is very good at processing large matrices of data.  Calculations can be performed in the command window but the quickest way is to bring in the data and run a script file with the appropriate calculations.  An application I am doing currently is taking seismic data and processing the large blocks of test data to find the desired patterns/parameters.  I’ve taken a break from doing anything in Matlab for a while now and I’ve had to relearn a lot of these tips recently, but these should speed up your script writing process quickly.

Use comments to section and annotate parts of code – An easy to read code is essential to speeding up the process.  And not only does it speed up the writing of the program, but gives you a reference later if you have to use it after several years.  Some good things to specify in my opinion are the author, purpose, input, out, and calculation methods/processes for each section.

Use help function, lookfor function, and the simulink website to get more information – Matlab has some great references in their program and on their website.  If some help is needed in using a function or finding a function, search for it and with a bit of reading you should be able to get moving in the right direction if you know the basics of coding.

Initially clear the screen and variables and close all figures – By clearing everything out, it makes it very easy to see the results or figure out what went wrong.  I can’t tell you how many times I have caused issue from old data being in my workspace or on my command screen, especially when starting out.  Save yourself the trouble and clean everything up before running the program; put those commands at the start of the script file if possible.

Import data from excel by using the import command, then save the workspace – If you have large amounts data to pull in, there is an import command you can use in the workspace area.  This speeds up the process of bringing in data exponentially.  If it’s not needed, don’t worry too much about it; but since I am assuming that a large amount of data is being used, this issue will come up eventually.  Once you have the variables set up like you want, save the workspace.  A command can then be used to open a file of the saved variables.  This whole process saves time entering the data and minimizes lines of code on the script file.

Be as patient and methodical as possible – The only thing worse than having to fix a mistake in a script file is having to fix a mistake in a large script file.  The best thing to prevent that from happening is to take the coding process one step at a time.  Write a few lines of code, check and see you are getting the results you want, then move on to the next few lines.  It will save you a lot of headaches in the long run.

Know when to simplify or allow for more complexity – When playing around with data (especially experimental data), there are a lot of methods you can use to calculate the desired results.  These methods can range from very simple to very complex.  The skill in properly using these methods and the different commands available in Matlab is knowing when to keep it simple and when it is worth it to try something more complex.  Doing something complex and having it work inconsistently is a waste and it would probably be better to simplify it.  However, if you see a pattern and something more complex that might work, it is worth to try doing it.

These are the basic concepts I have learned in using Matlab.  There are more specific practices that are good to know as well, and once you get better you will learn those as well hopefully.  Jumping in with very little experience though, I don’t think it is good worry too much about the specifics because it would become too confusing.  Focus on these tips and pick up the rest with time and practice – not to mention I probably don’t know well enough to teach you them either.  Along with that, there are other standard things I do and with time each person will develop their own as well.  Do what you think works well and don’t stress out if someone else does or doesn’t do it until it becomes a critical issue.  Coding is a lot like writing in that everyone has a unique voice and once you embrace your style it goes a lot smoother.

I hope these tips help with you Matlab coding.  What are some basic tips that you have for coding in Matlab?  Are there some common mistakes you make that might be considered basic?  Are there any important mistakes that I missed?

Advertisements

Single Post Navigation

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

%d bloggers like this: