LearningMatlab - MRC CBU Imaging Wiki
CbuImaging: LearningMatlab

matlab for SPM

To attain the glittering heights of becoming an SPM power user, you will need to learn some matlab programming. SPM is written in matlab, which is a high level programming language based on matrices. One of the the virtues of SPM is that the authors give out the source code, which is mainly in the matlab programming language. This means that, if you can program in matlab, you will be able to read the code to see what SPM is doing (it is quite often the only way to work this out). You can also change the behaviour of SPM by changing the code, or write code that works with SPM to do analyses that SPM does not do, or pull data out of SPM to analyse with other packages.

Versions of matlab and SPM

There are two basic versions of SPM in current use: SPM2 and SPM5. SPM2 will run on matlab 5.3 or greater. I have used it with matlab 7 at least. SPM5 is recorded as needing matlab 6.5 to 7.1

Starting matlab

This may depend on your local system. On the MRC-CBU system, we start matlab in Unix, by typing, simply,

matlab

at the unix prompt. This brings up the matlab window, which is a text input window, with a prompt that looks like this:

>>

To run matlab programs, you can type the names of matlab scripts or functions at this prompt. For example, to get the SPM2 file selection dialog box, you could type:

spm_get

at the matlab prompt.

The equivalent for SPM5 is

spm_select

Matlab very basics

With the matlab window, you have access to a very powerful mathematical system. At its most basic, you can use it as a calculator. For example, if you type:

36*67

at the >> prompt, and press return, you will get the following output in the matlab window:

ans =

        2412

Like other programming languages, matlab uses variables. For example, to set the variable A to equal 36:

A=36

which will give us the output:

A =

    36

if we then set B thus:

B = 67

then, as you would expect, you can type:

A * B

and get the output:

ans =

        2412

That was very basic, wasn't it? So, now would be a good time to have a look at some matlab tutorials. Below are some links to matlab information on the web. There is a good tutorial accessible via the Getting started link in the matlab HTML help system. You can get the HTML help system by typing "helpdesk" at the matlab prompt. You can get another version of the same tutorial by typing "tour" at the matlab prompt. See below for other help options within matlab.

Matlab tutorials

There are a large number of matlab tutorials on the web. A good place to start is the Indiana University Matlab page, which contains a basic tutorial and lots of links to other tutorials.

Below is an unsorted list of other matlab links that are mainly basic tutorials:

Getting help from within matlab

The following commands may be useful to get help within matlab. To use them, type the commands at the matlab prompt.

help

lists the topics for which help is available.

help

followed by a matlab function name, such as, for example, spm_get - the SPM2 file selection function, prints out the help available for that function in the matlab window.

intro

starts an introduction to matlab.

doc

starts the matlab HTML help system.

xplang

starts an introduction to programming in matlab.

lookfor

followed by some word of interest, searches all the directories matlab knows about for functions with the word in the first line of help for that function. Try:

help lookfor

for more detail. There are a couple of very useful additions to the help documentation. Firstly, the

doc

function, that can also be reached by the command

helpdesk

As I mentioned above, there is a useful tutorial attached to the "getting started" link on the start page of this help system. There is also another help system, rather simpler in design than the HTML help, reached with the command

helpwin

Printed manuals

Don't forget these. I found the introductory user guide for 4.2 helpful, all that time ago. I haven't looked at the equivalent manuals for later versions of matlab.

Using matlab with SPM

So, you now have a basic grounding in matlab. Using matlab with SPM is broadly pretty simple, but, as with SPM generally, it is very important to understand the underlying concepts. To get started, the first thing to do is to understand the image format used by SPM. This is a modified version of Analyze 7 format; you can find details in our Analyze format web page. Next you could try my SPM statistics tutorial, and in particular the associated matlab code, http://imaging.mrc-cbu.cam.ac.uk/scripts/statstalk.m. This code takes you through getting data out of image files, doing some basic adjustments to the data, plotting it, saving it to file etc. It might be useful to work through this code with the tutorial to get a feel for programming with matlab and SPM. If you want to write programs using the SPM interface, have a look at the spm_get routine (SPM2) or the spm_select routine (SPM5), in the SPM distribution, and the spm_input routine. Basic help is available via:

help spm_get
help spm_select
help spm_input

The code itself contains more comprehensive help; try looking at spm_get.m / spm_select.m and spm_input.m in your favourite text editor.

MatthewBrett

CbuImaging: LearningMatlab (last edited 2013-03-07 21:23:05 by localhost)