P1 = spm_select(Inf,'image','Select first time points');
P2 = spm_select(size(P1,1),'image','Select second time points');

for i=1:size(P1,1)

        ref = deblank(P1(i,:));
        mov = deblank(P2(i,:));
        [pth,nam,ext] = spm_fileparts(ref);
        c1   = fullfile(pth,['c1' nam '.nii']);
        
        odir = pth;
        change = ['change_' nam '.nii'];
        
        [pth,nam,ext] = spm_fileparts(mov);
        jac = fullfile(pth,['jy_' nam '.nii']);
        
        matlabbatch{1}.spm.tools.hdw.data.ref = {ref};
        matlabbatch{1}.spm.tools.hdw.data.mov = {mov};
        matlabbatch{1}.spm.tools.hdw.bias_opts.nits = 8;
        matlabbatch{1}.spm.tools.hdw.bias_opts.fwhm = 60;
        matlabbatch{1}.spm.tools.hdw.bias_opts.reg = 1e-006;
        matlabbatch{1}.spm.tools.hdw.bias_opts.lmreg = 1e-006;
        matlabbatch{1}.spm.tools.hdw.warp_opts.nits = 8;
        matlabbatch{1}.spm.tools.hdw.warp_opts.reg = 4;
        matlabbatch{2}.spm.tools.preproc8.channel.vols = {ref};
        matlabbatch{2}.spm.tools.preproc8.channel.biasreg = 0.0001;
        matlabbatch{2}.spm.tools.preproc8.channel.biasfwhm = 60;
        matlabbatch{2}.spm.tools.preproc8.channel.write = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(1).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,1')};
        matlabbatch{2}.spm.tools.preproc8.tissue(1).ngaus = 2;
        matlabbatch{2}.spm.tools.preproc8.tissue(1).native = [1 1];
        matlabbatch{2}.spm.tools.preproc8.tissue(1).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(2).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,2')};
        matlabbatch{2}.spm.tools.preproc8.tissue(2).ngaus = 2;
        matlabbatch{2}.spm.tools.preproc8.tissue(2).native = [1 1];
        matlabbatch{2}.spm.tools.preproc8.tissue(2).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(3).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,3')};
        matlabbatch{2}.spm.tools.preproc8.tissue(3).ngaus = 2;
        matlabbatch{2}.spm.tools.preproc8.tissue(3).native = [1 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(3).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(4).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,4')};
        matlabbatch{2}.spm.tools.preproc8.tissue(4).ngaus = 3;
        matlabbatch{2}.spm.tools.preproc8.tissue(4).native = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(4).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(5).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,5')};
        matlabbatch{2}.spm.tools.preproc8.tissue(5).ngaus = 4;
        matlabbatch{2}.spm.tools.preproc8.tissue(5).native = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(5).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(6).tpm = ...
        
        {fullfile(spm('dir'),'toolbox','Seg','TPM.nii,6')};
        matlabbatch{2}.spm.tools.preproc8.tissue(6).ngaus = 2;
        matlabbatch{2}.spm.tools.preproc8.tissue(6).native = [0 0];
        matlabbatch{2}.spm.tools.preproc8.tissue(6).warped = [0 0];
        matlabbatch{2}.spm.tools.preproc8.warp.reg = 4;
        matlabbatch{2}.spm.tools.preproc8.warp.affreg = 'mni';
        matlabbatch{2}.spm.tools.preproc8.warp.samp = 3;
        matlabbatch{2}.spm.tools.preproc8.warp.write = [0 0];
        matlabbatch{3}.spm.util.imcalc.input = {c1,jac};
        matlabbatch{3}.spm.util.imcalc.output = change;
        matlabbatch{3}.spm.util.imcalc.outdir = {odir};
        matlabbatch{3}.spm.util.imcalc.expression = 'i1.*(i2-1)';
        matlabbatch{3}.spm.util.imcalc.options.dmtx = 0;
        matlabbatch{3}.spm.util.imcalc.options.mask = 0;
        matlabbatch{3}.spm.util.imcalc.options.interp = 1;
        matlabbatch{3}.spm.util.imcalc.options.dtype = 4;
        spm_jobman('run',matlabbatch);
end

