Saturday, November 10, 2012

SPM Jobman

Now that we have created our own .mat files from the SPM GUI and seen how it can be written to the disk, altered, and reloaded back into SPM, the hour is at hand for using the command spm_jobman. This is a command for those eager to disenthrall themselves from the tyranny of graphical interfaces through batching SPM processes from the command line.

I first met spm_jobman - also known as Tim - a few weeks ago at a conference, when I was at the nadir of my sorrows, despairing over whether I would ever be able to run SPM commands without the GUI. Suddenly, like a judge divinely sent in answer to the lamentations of the oppressed, spm_jobman appeared by my side, trig and smartly dressed, and said he would be more than happy to help out; and from my first impression of his bearing and demeanor, I believed I was in the presence of an able and reliable ally. Anyone who has ever met spm_jobman, I believe, has felt the same thing. However, as I learned too late, far from being a delight, he is a charmless psychopath; and he continues to infect my dreams with nameless horrors and the unrelenting screams of the abattoir.

spm_jobman has three main options to choose from: Interactive, serial, and run. After choosing one of these options, for the second argument you enter your jobs structure, which is automatically populated after loading the .mat file from the command line. Interactive will load the traditional GUI with the options filled in from the jobs structure, which you can then modify and execute as you please; Serial will prompt the user to fill in each field, with the defaults set to the values in the jobs structure; and Run will execute the jobs structure without cuing the GUI. For most purposes, if you decide to run spm_jobman at all, you will want to use the Run command, as this allows you to loop processes over subjects without pause, allowing you to do more useful tasks, such as Googling the history of the lint roller.

Saving .mat files from SPM is immensely helpful in understanding the relationship between the .mat files created by SPM, and what exactly goes into them; and this will in turn reinforce your understanding of and ability to manipulate Matlab structures. The following tutorials show how the .mat file is generated from the SPM interface, which can then be used as a template for spm_jobman. I've been working with SPM for years now, but found out about this only recently; and I hope that it helps ease the burden of your SPM endeavors.


  1. I feel your pain. I appreciate your candor. Most of all, thanks for the tips. Now back to my dark cave for a conversation with jobman, fighter of the guiman.

    I really should have just paid the troll toll.

    1. SPM doesn't make it that easy to script things, but once you start to get the hang of it - once you spend countless hours messing around with .mat files and Matlab structures - then, slowly, imperceptibly, it all starts to make sense. Which is one of the signs that you are beginning to go crazy.



  2. Hi Andy,
    Bassically, using the GUI, some command such as Normalization (Write option) accept one image by sn.mat normalization files.
    But I would like to normalize few images (MGE images with different TE) with the same sn.mat file and I have 45 subject....
    Do you know the way to loop the normalization function in order to normalize a set of images with the same sn.mat file ??

    Thank you


  3. Thank you so much, Andy. I watched your spm_jobman youtube tutorial and found it very helpful. However, I'm trying to run a batch file saved as a .m file. Would the procedure be the same? I received an error and don't know why.

    1. Hey there,

      When you save the processing script as a .m file, you see two different files: file.m, and file_job.m. All of the fields in file_job.m are filled in with the values you entered in the GUI, and file.m executes file_job.m using spm_jobman. In that sense, the procedure is basically the same.

      What kind of error are you getting?


  4. Hi Andy! Thanks so much for your videos and posts! I am trying to run a batch for a quality analysis using the toolbox "tsdiffana" for SPM12, but when trying to load the "batch_tsdiffana.m" I get this error: Undefined function 'list' for input arguments of type 'cell'. I do not understand where the error is, especially because there is no such a function when I look at the batch script.

    Could you please help me with your thoughts? Thanks a lot!