SPM/Programming intro

Aim
This page is intended to provide a quick-start guide to writing your own MATLAB scripts and functions using SPM as a library. SPM programming can mean simply writing batch scripts to automate common pipelines, writing short helper scripts or functions to accomplish useful tasks, writing your own SPM extensions, or even modifying your local installation of SPM.

MATLAB
MATLAB is a programming language developed by MathWorks. Many MATLAB tutorials are available online: There are also useful books: MATLAB courses:
 * MATLAB and MathWorks on Wikipedia
 * MATLAB on WikiBooks
 * MATLAB Tutorial
 * MATLAB for Neuroscientists
 * MATLAB for Psychologists
 * MATLAB for Psychology and Neuroscience short course in Nottingham
 * MATLAB for Psychologists short course in London

Introduction
SPM has extensive developer documentation in the headers of the source files. To view the documentation of a function either open the corresponding source file or call  from the MATLAB command line. Make sure that the SPM folder is in MATLABs search path.

Reading image headers and data

 * – header information
 * – for reading entire volumes (see also: )
 * – for arbitrary planes
 * – any voxels
 * – any voxels from multiple volumes
 * – NB  and   offer polynomial or sinc interpolation; these functions provide b-spline interp as used in

Geometry, voxel-world mappings

 * – get the voxel-world mapping matrix (a rigid or affine transform, in homogeneous coordinates)
 * – convert above matrix to parametrised form
 * – convert parameter vector to affine matrix

Linear (rigid/affine) registration and reslicing

 * – needs reference image; see following for reslicing to specified geometry
 * – available from John's Gems 2 and 3
 * – needs reference image; see following for reslicing to specified geometry
 * – available from John's Gems 2 and 3
 * – available from John's Gems 2 and 3

Preprocessing, including segmentation and non-linear normalisation

 * – SPM5's unified segmentation and normalisation
 * – the old pre-SPM5 non-unified spatial normalisation
 * – the old pre-SPM5 non-unified tissue segmentation

Processing

 * – perform arbitrary calculations on volumes (low level function)
 * – high level convenience wrapper for

Statistics

 * – unused by SPM itself, but often useful for scripting or educational purposes

Viewing data

 * – the ubiquitous three orthogonal views
 * – slices through images, overlays of thresholded or raw statistics; see also slice_overlay
 * – maximum intensity projections or glass brain images

Reading and writing a volume (to replace NaNs with zeros)
A simpler (but more memory-hungry) version of an old Gem. See the SPM8 version of gem for an example of plane-wise reading and writing. fnm = spm_select(1, 'image'); [pth, bnm, ext] = spm_fileparts(fnm); VI = spm_vol(fnm); VO = VI; % copy input info for output image VO.fname = fullfile(pth, [bnm '_zn' ext]); img = spm_read_vols(VI); img(isnan(img)) = 0; % use ~isfinite instead of isnan to replace +/-inf with zero spm_write_vol(VO,img);