MATLAB for physiological analysis

I use MATLAB for my number crunching. While data acquisition and analysis programs such as Spike and LabChart have analysis functions that are good, they are not always appropriate for the type of analysis required. Also, I like to know all the calculations made on my raw data, and so my analysis tends to gravitate towards custom-made code. Dealing with time series for the most part, I’ve found MATLAB very useful, but it’s also handy for other types of data.

I therefore thought I’d compile a small list of things that has worked for me for the MATLAB novice looking to start using the program.

Learning MATLAB:

  1. Start small. Create a small matrix (e.g.20×20). Write code for processing your small matrix and make sure that it does what you expect it to do. Learn the basics on this matrix before touching real data.
  2. Break it. Error messages are great. Each message will lead to a better understanding of how to use MATLAB, so make sure you understand what they mean.
  3. Use the forum. MATLAB central will have many (if not all) the answers you can wish for (including: “what does my error message mean”). It’s a great resource! Link: http://uk.mathworks.com/matlabcentral/

Using MATLAB:

  1. Remember that MATLAB is only as good as its user. In many cases, you’ll get a number which might appear reasonable, but errors in your code can still be present. Getting an output does not mean that your code works the way you want.
  2. Learn to plot your data. Plot raw data and superimpose your calculations along the way. Mistakes should be easy to visualise, at least when working with time series. This is an easy quality check and has spared me some embarrassing mistakes in the past. Also, the figures you get can be made quite attractive for publication purposes. Certainly prettier than MS Excel, at least.
  3. Run the code on a known data set first. Make sure that the code produces the results you would expect. Again, this is a simple thing that can spare you a lot of grief. Compare with in-built programs if possible (such as spike detection in LabChart versus custom-written spike detection in MATLAB).
  4. Understand which computations are better done manually. Small, one-off computations could require more work coding up than just processing manually. The point of the exercise is to make things easier, not harder.
  5. Learn how to run a series of analyses with one click. Sometimes you’ll need to run many subjects in quick succession. Writing code that reads a list of subject numbers (one click) and automatically loops through the analysis process for each line on the list is one way of doing this that has saved me some time in the past.

Automated analysis can be very useful as it is less time consuming in the long run and you reduce the risk of silly, manual mistakes (typos etc.). With a few clicks, you could run all your number crunching in the background whilst you have your morning coffee and try to wake up your brain. Sounds good, eh? It also helps you understand the data better. For example, if you want to calculate a waveform mean, you have to know the mathematical formula for doing so. In an in-built program, this would not be needed. In short, it can be very helpful, when done right.

A few other things:

  1. Text files are good. Text files (.txt or .dat) are quite easy to work with in MATLAB. Save raw data in one of these simple formats as well as the custom format.
  2. Company is better. If there is someone else around that knows or wants to learn, join forces. It’s much more fun that way.
  3. Annotation is the best. Annotate and annotate well. Your future self thanks you! Trust me on this. A few simple %this-is-what-this-line-does will make your life infinitely easier.

If you’re not certain about using MATLAB, there’s a 30-day trial version available that you can play with to figure out if it is for you. It’ll take a little while to get to grips with if you’re not used to this type of program, but it’s worth the time.

161px-Matlab_Logo