The information on this page is probably not relevant for new projects, but may still be helpful to some. Still, it is a lot of information, so I'd hate to just delete it when it might be useful to someone, somewhere.  Dianne Patterson 10/25/2006

Installing SPM 99

You can only install SPM99 on a machine that has Matlab, because it is primarily a collection of Matlab routines (*.m files). SPM99 runs on Windows, Redhat linux and Suns without any compilation. On sgis, it will need to be compiled. To compile SPM99, read the spm_MAKE.sh file in the spm99 directory. You will need to run a command like >./spm_MAKE.sh gcc (to install on linux; modify spm_MAKE.sh to point to your correct gccopts.sh in the matlab bin). With some patience, SPM99 can be installed on sgis, but you must have a C-compiler and be prepared to alter the C-compile options in spm_MAKE.sh and /usr/bin/matlabXX/mexopts.sh, depending on whether you have matlab5 (which uses the old 32 bit libraries) or matlab6 (which uses the new 32 bit libraries).

If you have trouble with speed, make sure you download and install the Matlab 6 from Mathworks. Simply find and replace the files on your system with the equivalent files from the download site.

Running Multiple Versions of SPM on the same Machine: If you want to run both versions of spm on the same machine, you'll run into matlab path problems. Instead of hardcoding the spm directories into matlab, you can use the spm2.m, spm99.m, and spm5.m scripts from Local Downloads to set the path to the appropriate directories for the duration of your spm session. Simply put spm99.m, spm2.m and spm5.m in the matlab path (e.g., in matlab/work). Remove matlab paths to your existing spm directories. Open spm2.m and spm99.m and check that the path to each of your main spm directories is correctly specified at the top of the script....try it out. You can add or remove paths to subdirectories of each spm directory as appropriate to your installation.


In the SPM99 downloads area, you will find fmri practice data sets ("Study1DataSet"and "DataSet2"); as well as scripts and data.

On Windows? Software like Iceows from http://www.iceows.com/HomePageUS.html will work on Windows systems to ungzip and untar files that might otherwise be difficult to handle.

Test Images (For Left and Right issues): You may also want to download the sample 2D, 3D Anatomicals and the Functional files. These contain images of a brain with a large obvious left frontal lesion. The 2D and Functionals are in radiological orientation. The 3D was acquired in the sagittal plane, Left to Right. These images are very helpful for determining and verifying what imaging programs are doing to the images when they convert them or flip them.

From SPM99 Downloads, go into the directories "Study1Dataset" or "DataSet2" for downloadable files related to either dataset OR click on the links below to get to the same files. Dataset2 was downloaded from the spm home site (http://www.fil.ion.ucl.ac.uk/spm/data).

Study1DataSet A Raw CNL dataset (Pre September, 2002 scanner format) and exactly the same as the dataset used for the CNL Afni tutorial, is available with a revised and improved tutorial document SPM99WorkbookStudy1.doc (WORD).

The functional data is available in several different stages of processing:
(Same as above, what you want if you need to start at the beginning and do all of the locally relevant steps...in case you are not at our site and don't have the grecon tools, etc.),
(The structural and Functional data prepared for SPM),
Realigned and Normalized
Smoothed, Normalized and Realigned
(ready for analysis).
Or, if you'd like the Pfiles in all states of processing (Realigned only, Normalized and Realigned, and Smoothed normalized and realigned) then you can get FuncPreprocessed.tar.gz.


Sets of Anatomical/Structural data are also available:
, (So you can start processing them from the beginning with all site local concerns)
Coregistered and Normalized.


If you have a basic knowledge of using SPM through the GUI interface, you may be interested in learning about batch processing:

Study1_batch_Data.tar.gz Flipped data copied into two separate run directories, and the flipped anatomy directory set up for the batch processing tutorial (see Batch Processing below)


DataSet 2: Advanced Event Related-Repetition priming


Converting P-files to ANALYZE Format
This should just take a few minutes..Get versions of phx_tospm here.

Convert P-files to ANALYZE format AFTER you have run grecon and rename, but BEFORE you start SPM. (prep scripts are available for running the appropriate grecons/rename sequence. For more information on prep scripts, click here).

Start matlab:
>matlab -nojvm

At the Matlab prompt type:

Note that if you are setting up Matlab and SPM99, you will need to make sure that phx_toSPM.m is in a directory that is in the Matlab path, for example, put it in the directory with the other SPM99 files.

Follow the instructions as prompted (see the following example). This conversion is done one "run" or scan at a time.
patient fmri file name (no .ext, eg. P03072): P01401
# of slices per volume per time point (eg: 23): 27
# of reps for this subject (eg: 225): 193
matrix size x (eg: 64): 64
matrix size y (eg:64): 64
slice thickness in mm (eg 6): 5
field of view in mm (eg: 220): 220

Faster Command line versions: On our CNL machines, you can run phx_tospm3.m (if you want to batch things up) or phx_tospm4.m if you want to batch them up and have them flipped in the y dimension. Learn more about creating a simple matlab script on the CNL Matlab page. Get versions of phx_tospm here.

Troubleshooting phx_tospm (any version):

  • If you have not used the correct version of grecons on your P-file, no version of phx_to spm will be able to reconstruct it correctly. Note that the tutorial data uses grecons5x, whereas the prep scripts use newer versions of grecons. To learn more about versions of grecons, go here.
  • If you have tried to run any version of phx_tospm and had it fail, make sure you remove any *.img and *.hdr files it produced before failure....else phx_tospm (any version) will continue to fail.
  • phx_tospm commands or scripts run very slowly if you attempt to run them in the directory with your thousands of image files. It is best to run the command from a directory that is less crowded. Here's an example of running phx_tospm4.m from the parent directory (and specifying the path in the command):

    >>phx_tospm4 ('Study1/P15872',17,80,64,64,5,220)

  • Keep in mind that you may want to run phx_tospm3 or phx_tospm4 in batch mode...learn more about creating a little batch script here on the CNL Matlab page.
  • It may be the case that restarting Matlab will help if the script won't run and you've tried other options (I don't know this for sure, but if you are annoyed, you might as well try it).
  • More than 9,999 Images in your P-file? Check out issues here and "b" versions of phx_tospm (b is for "big").

Flip Images
This assumes you are now in SPM99: To flip the images, SPM must create a *.mat file for each image file, because the matrix files store the transformation information. In this case, realignment will not need to create the mat files, and should thus be faster. The flipping process will take a very long time.

Pull up one of the images
Press <DISPLAY>, highlight an image on the resulting menu, and press <DONE>
The displayed image should appear in 3 panels: "coronal" on the upper left (initially upside down),
"sagittal" on the upper right (also, initially clearly upside down)
"axial" on the bottom left...this one will look just fine, like an egg with the narrow end up. It doesn't get changed.
In the resize boxes, change both the Y and Z axes from "1" to "-1".
(if images are already right-side-up, just change Y to "-1")
Press <REORIENT IMAGES>, choose the images for 1 run, and press <DONE>.
Choose one image: press <DISPLAY>, highlight an image, and press <DONE> to confirm successful flip (look at the saggital and coronal).

Getting *.MR Files ready for SPM99

Go into the directory that contains your anatomy files (these should not be renamed or changed in any way from their initial form) and start matlab:

>matlab -nojvm

In matlab, call either the t12spm.m (if your voxels are .859 .859 6) or t3d2spm.m (if your voxels are .859 .859 1.5) function with the following parameters. These files assume an offset of 7904 (pre-September, 2002). For information about 2002/2003 scanner upgrades and related changes to file formats, go here.


>>t12spm ('E21670S2I',17,2,'brain.img','no',1,17)
(This will create the "2D" structural image; t12spm is a modification of efl2ana. t12spm has the voxel size for our T1s and also calls a script to strip off the .MR from each anatomy file. It is installed on holly, buddy, and merlin. If you are working elsewhere)
>>t3d2spm ('E21670S4I',124,2,'brain3d.img','no',1,124)
(This will create the "3D" structural image)

filebase will be something like e44889s2i
num_sli: total # of slices (e.g. 17)
byte=2: "save as two byte data" (Our MR images are usually 2 bytes of image depth); 1: means "save as one byte data"
raw_mriana: analyze file name
flip='no': do not fliplr and flipud
'yes': do flip
ssn: start slice # (optional)
esn: end slice # (optional)
if ssn and esn are not given, they will be interactively determined

Data Collected at the Uof A after September, 2002? Check out the new header size and it's implications here. See the Updates page for information about t12spm_new and t3d2spm_new. Files that don't include .MR have the filebasename specified with a final dot ".", so you should really check it out.

Batch Processing in SPM99

SPM99 must be installed and in your Matlab path. I recommend spm99_kul.tar.gz from http://www.kuleuven.ac.be/radiology/Research/fMRI/kulSPM/ for batch processing.
These researchers have created function calls to the underlying spm matlab routines that are reasonably easy to understand. You will need to unzip and untar their directory of m-files and add that directory to your matlab path. In their directory are several files called example_script_ xxx and each of these contains a set of calls to their functions. The following tutorial BatchProcessingExplained.doc tries to expand on the Kuleuven materials by examining in detail the preprocessing and analysis choices made for the tutorial data in preproc_study1.m, stat_study1_simple.m and stat_study1_fancy.m.

The Tutorial Data: I have copied the Kuleuven script "example_script_preproc.m" to create preproc_study1.m (Windows version; download separately) and altered it to do all of the preprocessing (plus slice timing) that is described in the SPM99WorkbookStudy1.doc for the study1 tutorial data. You MUST modify the paths in preproc_study1.m to make it work on your machine, including a unix machine.

The data for the Study1 batch processing tutorial is Study1_batch_Data.tar.gz. Download the data and untar and unzip it (you can do this on Windows if you go find a smart unzipping utility, you might try Iceows which works well for me but not for everyone. Other freeware and commercial zip programs are available). The data *should* extract into a folder (study1) with three subfolders (run01, run02 and anato)....but does not seem to always cooperate.

Alternatively, you can download run01.zip, run02.zip and anato.zip: run01.zip, run02.zip, anato.zip. Then unzip them and put them into a directory called Study1.

Place preproc_study1.m in the matlab path. Read and alter preproc_study1.m: Make sure the path to the Study1 data directory and the spm template directory are correct in the script (both are defined at the top of the script). Check through the entire script for path names. Remember the directions of the slash in a path is different for windows "\" and unix "/". If everything has been done correctly, you should be able to type:


This will automatically run in simulation mode which takes a minute or two. Read the output to identify any errors. If there are no errors, then IN THE SCRIPT change fmriTEST = 1; to fmriTEST = 0; This should now run through all the preprocessing steps creating the necessary files.

There are other example_script files (in the spm99_kul directory you downloaded from the Belgian site) which you might wish to explore, once you are done with this one.

The model specification and estimation script (example_script_stat.m) has also been modified to be run with the tutorial data: stat_study1_simple.m and stat_study1_fancy.m to imitate the analyses described in SPM99WorkbookStudy1.doc.

Your Data: When you are ready to try your own data, keep the following in mind:The functions are dependent on a typical organization of the images. The anatomy directory and any runs for that subject should have separate subdirectories with names of the same length (e.g., for the tutorial: "run01", "run02", and "anato" each 5 characters and all in Study1). Use simulation mode until you get no errors. The authors advise checking the results of your batch process against the same data processed by hand, to make sure you are doing the same things.


As we actually try to run scripts, we discover strange behaviors which I report here, in the hope that they may help you troubleshoot your own problems:

1) First, note that the behavior of the scripts varies in subtle ways with the operating system and the version of matlab (e.g., the normalization basis function gets an error running prod if you use Matlab 6.5 but works fine with Matlab 6.0) and probably which patches you have in spm99. The following observations are for Matlab 6.0 (Release 12) running on Windows XP or 2000 with a fully patched spm99 (up-to-date for June 2003)

2) Paths

fmriDIR = 'C:\Study1'; This line works

fmriDIR = 'C:\Study1\'; This line does NOT work

fmriDIR = 'C:\Study1\run01'; If your data is in run01, make sure you do not specify that as your fmriDIR, the fmriDIR should be the parent directory to run01.

3) Capitalization matters, even on Windows. And, when you move files from unix to windows...windows sometimes decides to disrespect your capitalization choices (it often changes the files to all lower case). Watch for this.

4) Some functions behave differently than others, slice timing accepts different TRs on unix vs windows. Slice timing (at least on Windows) will not run properly in simulation mode UNLESS the files it is looking for actually exist. Other functions seem to handle simulation mode correctly for files that have not yet been created.

5) Coregistration seems not to run except in context (It seems to work if you run the normalization step in simulation mode first, but not if you don't...it is not just a matter of loading some variable, rather, normalization seems to change the environment in some way that "clear" [typed at the Matlab prompt] does not change, but "clear all" does change).

6) Coregistration does not seem to take a wildcard (*) as part of a filename, even though other functions do.

7) On Windows Machines, avoid using path names with spaces on them. Matlab will generally explode in confusion if it has to follow such a path.