Selecting files in SPM¶
Any time you select files or directories in SPM, the same file selection dialog box is used (with slightly different configurations). It is relatively straightforward, but there are a few tips and tricks that let it be used much more efficiently.
Using regular expressions to filter files¶
Many people are familiar with Unix-style file “globbing” in which certain characters have special meaning; for example, specifying f*
refers to all files starting with the letter “f”, regardless of what comes after.
SPM’s file filter uses a related, but more powerful, syntax called regular expressions. There are numerous guides to regular expressions available online, and the number of complex (and potentially useful) options made available is endless. However, here are a few key bits of knowledge, and some examples.
.
- A period (
.
) means “any character”. Filtering for.....
would give any file names that are 5 characters long (not particularly useful). Note that if you want to include a period in your expression, you need to escape it with a backslash (\.
). *
- An asterisk (
*
) means “0 or more of the preceding character”. Filtering for.*
filters for any file that has 0 or more characters (i.e., all files)—this is SPM’s default filter. ^
- A caret (
^
) means a string begins with the pattern that follows. Filtering for^f
gives all files that start with the character “f”. $
- A dollar sign (
$
) means that whatever precedes it is the end of the file. Filtering for\.nii$
will give all files with the extension “.nii”.
These can be combined nearly infinitely, which is what really unlocks the power of regular expressions. Thus, filtering for ^f.*\.nii$
will return files that start with “f”, have any number of characters in the middle, and end with “.nii”.
Recursive file selection¶
Recursive file selection (which happens when you click the “Rec” button) can be very useful when selecting images from multiple directories. Recursive selection finds files in subdirectories of the current directory (and in their subdirectories) that match the current filter. For example, if every subject has a “structurals” directory, and the filter “^s.*.nii” would find it, you could (a) navigate into each subject’s structural directory and select it, or (b) simply press the “Rec” button from a higher-level directory.
Selecting volumes from 4d files in SPM¶
The SPM file select dialog, by default, shows only the first volume of Nifti files. For 3d Nifti files, this is appropriate, because there is only a single volume. However, for 4d Nifti files, you’ll often want to select multiple volumes.
The volumes that SPM shows are specified by the “frames” parameter, by default set to 1. (Note that this field is not labeled in the spm_select dialog box, but is the field below the filter field.) The frames field works as a Matlab range selector, so to show volumes 1 through 10, set it to 1:10
. There’s not problem setting the upper number to a higher-than-needed value; so if a 4d Nifti file has 10 volumes, you could set it to 1:10
or 1:5000
with equivalent results—the upper number merely provides an upper bound (volumes beyond that won’t be displayed).