Simple calculations

If you have two files that are in EXACTLY the same space (i.e., exactly the same number of slices, voxel sizes, orientation) then you may perform simple mathematical calculations. For example, the following code loads two files, adds them and saves the result to a new file.

V1=spm_vol('myfile1.nii');
V2=spm_vol('myfile2.nii');
Y1=spm_read_vols(V1);
Y2=spm_read_vols(V2);
Y3=Y1+Y2; % only ever do this if they're in the same space
V3=V1;
V3.fname='outputfile.nii';
spm_write_vol(V3,Y3);

If they're in a different space, you will either need to reslice one to the same space as the other (see command spm_reslice) or to resample one as you load it up, with spm_sample_vol