Matlab (Matrix Laboratory) is a mathematical programming language, primarily for manipulating matrices of numbers. Matlab is a VERY powerful tool for signal processing. Most MGH and SPM functions are Matlab m-files (txt files with the extension .m). Matlab works primarily with two types of files:

*.m files (These are programs or scripts that operate on matrices of numbers; they are regular text files and can be edited in any text editor). m-files for MrGray. m-files for SPM.)

*.mat files (These are matrices of data that the program works on.)


  • Matlab Home
  • The Joy of Matrices (Good Reading)

  • Local Resorces (a simple user interface and some matlab files to do low level image processing- masking, descriptive stats, plotting, movie and still image viewers). Tutorial in Word format, MatlabImagingTutorial.doc; some simple image processing m-files,; and datasets, and (the normalized functional *.img files). I cannot gaurantee the mfiles are without error. You may also be interested in the two sets of Left Lesion Structural Data (256x256x17 and 256x256x124 respectively) in *.img format.

    If you want to run through the tutorial, I suggest you download the doc file,, and the dataset to begin.
  • Analysis Tutorials Rajeev Raizada's excellent Matlab tutorials explaining hrf, convolution, the design matrix, etc. for image processing programs, with a focus on spm.
  • A resource for M-files and program add ons for other languages.
  • cell arrays (a simple description of cell arrays and how to manipulate them, als in the tutorial)


You can examine and modify an m-file by opening it with ANY text editor. Matlab 6 has an m-file editor (which color codes keywords and comments). The m-file editor can be accessed, after starting Matlab, by choosing File-->New-->Mfile (then use File-->Open in the editor to find your file). You should be aware, however, that most m-files are in areas that require root write you can read them, but you cannot modify them. If you want to create and test your own m-file, put it in the same folder where you open Matlab (often the same folder as your data).

Warning: Never name an m-file with a number as the first character in the simply won't run.

The simplest kind of m-file is one that calls other m-files and provides any necessary arguments. Such an m-file functions like a batch file. It will call a function repeatedly with different arguments:

%Here is a simple matlab file. A line beginning with a % is a comment (like this one).
%Comments appear in green in the Matlab Editor.

%Call this "batch1.m". It calls phx_tospm3 with the appropriate arguments.
phx_tospm3 ('P15872',17,80,64,64,5,220);
phx_tospm3 ('P15873',17,80,64,64,5,220);
%This is the end of the file

The crucial lines in the above file are:

phx_tospm3 ('P15872',17,80,64,64,5,220);
phx_tospm3 ('P15873',17,120,64,64,5,220);

Nothing else needs to be there (but it is good practice to include informative comments in your programs). To run this file, I go to the directory that contains it, start Matlab and type:


To test an m-file that you are editing, save it (you do not need to close it). Then you can try calling it again at the matlab prompt. If your m-file starts to run and you want to stop it to make further modifications, type Ctrl-C. If the program fails, it will give you an error message and it will usually stop (as most errors are fatal). Pay attention to this error message when you are trying to debug. You can specify a path to the P-files you want to modify in your *.m "batch" file, rather than being required to be in the same directory as the data. So, for example, the following should work to process sets of P-files in different directories:

phx_tospm3 ('/data3/HOWTO/SPM/Study1/P15872',17,80,64,64,5,220);

phx_tospm3 ('/data3/HOWTO/SPM/FakeStudy/P15873',17,120,64,64,5,220);

To get help, type "help" followed by the function name (the name of the file, without the .m at the end) at the Matlab prompt:

>>help phx_tospm3

This matlab m-file converts greconed, renamed p-files into analyze format. It combines the P-files into sets (one set for each TR). It names them *.img files and creates a *.hdr file for each one. phx_tospm3 takes the following command line variables in order:

pname=patient fmri file name (no .ext, eg. P03072)
nslices=# of slices per volume per time point (eg: 23)
nruns=# of reps for this subject (eg: 225)
xdim= input('matrix size x (eg: 64)
ydim= input('matrix size y (eg:64)
sthick=input('slice thickness in mm (eg 6)
FOV = input('field of view in mm (eg: 220)


phx_tospm3 ('P15872',17,80,64,64,5,220)

You can also run phx_tospm3.m (for example) directly at the Matlab prompt by typing in the name of the function followed by its arguments in parentheses. A comma OR a comma and a space can be used to separate arguments:

>>phx_tospm3 ('P15872',17,80,64,64,5,220)

If you wish to have the images automatically flipped in the y dimension when they are converted to analyze format, you can use phx_tospm4.m in exactly the same manner as phx_tospm3.m (you can call it from a batch file, it takes the same arguments etc, it simply adds a line to do the flipping). 

Please note that all of this is now accomplished by our prep scripts on unix.


To view a *.mat file, you must first load it:

>>load test.mat

To display the contents of a mat file (since it can contain multiple named matrices):


To display an individual variable, type the variable name at the Matlab prompt.

To examine the contents of one of the variables listed by "who", simply type its name, for example a variable called M holds the 4x4 matrix used by spm:

>> load P15872em1.mat

>> M

M =

-3.4375 0 0 111.7188
0 3.4375 0 -111.7188
0 0 5.0000 -45.0000
0 0 0 1.0000

Here we see the Y dimension flipped (-3.4375, and a positive number in the 4th column)
The X dimension is not flipped (second row)
The Z dimension (5.0000) is also not flipped (it is positive)

To unload everything from the workspace after looking at it:

or you can clear an individual variable:

>>clear M


The Matlab Path

Operating systems typically have a path where they look for programs. Matlab does not use the operating system to define its path, it defines its own path. When you add a new directory full of m-files and mat files, you need to make sure that directory is in your matlab path. In Matlab 6.0 and later, simply choose file (from the upper left of the gui) and choose "Set path". An interface will pop up that allows you to add a path. Read your online help or manuals for more information.

The License Manager

Looking at the STATUS of the license manager:

STARTING the license manager (as root):
>/usr/local/matlabr12/etc/lmstart -u dpat

Sometimes matlab won't start for one user, but is okay for other users. In this case, delete the hidden directory ".matlab" from the home area of the affected user and try again. Note that when you look at the status of the license manager: lmgrd is the main flexlm daemon and MLM is the matlab vendor daemon. MLM is what gets started once for every license you have for the machine...obviously if too many vendor daemons are checked out and running, they may have to be killed in order to start a new process. Running the lmstart process will kill running daemons, and you may need to run lmstart repeatedly to get to the point where matlab will run again. Look at the log file each time you try will give you feedback about what is happening: /var/tmp/lm_TMW12.log.

If you try to start matlab and it can't find the *.arch file...then you may be logged in as a user that doesn't have the path to matlab in their .cshrc. Try starting matlab with its full path:

> /usr/local/matlabr12/bin/matlab