fMRI预处理batch

fMRI 预处理code 好像暂时用不着

[转自52brain] 原文

addpath('C:/kongliliushuang/ImagingData');   %在这个路径中有SPM包中没有但是你要用的函数,比如spm_get
cwd='C:/kongliliushuang/ImagingData/preprocessing/'; %预处理的工作路径,里面放FunRaw和T1Raw
cd ([cwd,'FunRaw']);
        subExpID=dir ('sub0*');     %读取被试编号,此处是在FunRaw这个文件夹下来列出所有被试的编号。这样做的好处是,你的被试编号可以很不规则。

%-----------预处理每一步骤的开关-------如果只想处理某一步,就把开关设为1。当然,前提是这一步所需要的材料已经生成。
creatFolder=1;
convertingFunct=1;convertingStruct=0;
sliceTiming=1;
realignment=1; 
coregistration=0;
segmentation=0;
normalization=0;
smoothing=0;
DARTEL_NormSmooth=1; %用DARTEL来进行标准化。如果此处开关打开(设为1),以上convertingStruct, coregistration, segmentation, normalization 和smoothing就应该关上(设为0)。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% 设置空文件夹装中间步骤数据 %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if creatFolder

        cd (cwd)
        mkdir('FunImgF');mkdir('FunImgAF');mkdir('FunImgRAF');mkdir('FunImgWRAF');mkdir('FunImgSWRAF');mkdir('DARTEL_FunImgSWRAF'); %create folders

        foldernames={'FunImgF' 'FunImgAF' 'FunImgRAF' 'FunImgWRAF' 'FunImgSWRAF','DARTEL_FunImgSWRAF'}; 
             for folder=1:size(foldernames,2)
                   folderdir=[ cwd sprintf(foldernames{folder})]

                      for wnumfolder=1:size(subExpID,1)

                            cd(folderdir)
                            mkdir (subExpID(wnumfolder).name);

                      end
             end
        cd (cwd)
        mkdir('T1Img');
        folderdir2=[cwd sprintf('T1Img')]
                      for wnumfolder=1:size(subExpID,1)

                            cd(folderdir2)
                            mkdir (subExpID(wnumfolder).name);

                  end

        cd (cwd)
        mkdir('DARTELT1Img');
        folderdir2=[cwd sprintf('DARTELT1Img')]
                      for wnumfolder=1:size(subExpID,1)

                            cd(folderdir2)
                            mkdir (subExpID(wnumfolder).name);

                      end          

end %end of switch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

spm_get_defaults             % SPM 默认值
global defaults
spm_jobman('initcfg');


nses=1;                   % sessions或runs的数量    


for sub=1:size(subExpID,1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%  将DICOM转换成Analyze格式 %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%convert structural images%%%%%%%%%%%%%%%%%
if  convertingStruct
      dirStruc=[cwd,'T1Raw/',subExpID(sub).name,'/']; %location of raw DICOM                 
      cd([ cwd,'T1Img/',subExpID(sub).name,'/']);  %location to export the Analyze
      ds=spm_get('Files',dirStruc,'*.ima') %list all DICOM file names, there is no spm_get inSPM8, but marsbar has that function.
      hdrs=spm_dicom_headers(ds);    %read DICOM header   
      spm_dicom_convert(hdrs,'all','flat','img');     % convert
      display(sprintf('Anatomical run has been finished.'));    % show when finished
end %end of switch


%%%%%%%%%%%%%%%%%%%%convert funtional image%%%%%%%%%%%%%%%%%%%%%%
if convertingFunct
         dirFunc=[cwd,'FunRaw/',subExpID(sub).name,'/']; %%location of raw DICOM   
         cd([cwd,'FunImgF/',subExpID(sub).name,'/']); %location to export the Analyze
         df=spm_get('Files',dirFunc,'00*.ima')  %list all DICOM file names.
         hdr=spm_dicom_headers(df); %read DICOM header
         spm_dicom_convert(hdr,'all','flat','img');  %convert
         display(sprintf('Functional run has been finished.')); %show when finished
end %end of switch

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% slice timing %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        

if  sliceTiming
         dirst=[cwd,'FunImgF/',subExpID(sub).name,'/']   %location of data to do slice timing (from last step). 
         st=spm_get('Files',dirst,'fSH*.img') %list all files started with fR
         jobs{1}.temporal{1}.st.scans{1}=cellstr(st);   %combine all runs, not to slice time seperately.  Be careful with {ses}


     jobs{1}.temporal{1}.st.nslices = 34;
     jobs{1}.temporal{1}.st.tr = 2;
     jobs{1}.temporal{1}.st.ta = 2-(2/34);
     jobs{1}.temporal{1}.st.so = [1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34];  %34 slices, interleaved, ascending
     jobs{1}.temporal{1}.st.refslice = 33;     %middle point of time or anatomy. For data collected from SIEMENS, 
              %the temporal middle point is 33 and 34. The spatial middle point is 17 and 18. 
              %It should correspond to the "microtime resolution"(also called t) of the 1st level statistical analysis
              %and "microtime onset"(also called t0).If there are 33 number of slices, t0=17.
              % When reference for slice timing is 33, the 33rd slice is collected the 17th. If the refernce slice is 17, then t0=9.
     jobs{1}.temporal{1}.st.prefix = 'a'; 
     spm_jobman('run',jobs);    %begin
     clear jobs;               %clear jobs in this part


                                           diroffile=[cwd,'FunImgF/',subExpID(sub).name,'/']   % do add a / in the end
                                           files=fullfile(diroffile,'afSH*')
                                           diroffile2=[cwd,'FunImgAF/',subExpID(sub).name,'/']
                                           copyfile(files,diroffile2)    %copy
                                           cd(diroffile)
                                           delete 'af*'
end %end of switch  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%  realignment  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                
if realignment
     cd([cwd,'FunImgSWRAF/',subExpID(sub).name,'/']); %motion pictures exported to the folder of the last step

         dir=[cwd,'FunImgAF/',subExpID(sub).name,'/']; %location of files realigned to (from last step) 
         p=spm_get('Files',dir,'afSH*.img') %list files started with af
         jobs{1}.spatial{1}.realign{1}.estwrite.data{1} = cellstr(p);      %combine all runs. Be careful with {ses}.

         jobs{1}.spatial{1}.realign{1}.estwrite.eoptions.rtm = 0;         %set refernce to the first slice
     spm_jobman('run',jobs);    % run job. motion pictures in the last run folder. mean is in the first run
     clear jobs;               %clear jobs

                                         diroffile=[cwd,'FunImgAF/',subExpID(sub).name,'/'] %add /
                                         files=fullfile(diroffile,'r*')
                                         files2=fullfile(diroffile,'mean*')
                                         diroffile2=[cwd,'FunImgRAF/',subExpID(sub).name,'/']
                                         copyfile(files,diroffile2)    %copy 
                                         copyfile(files2,diroffile2)
                                         cd(diroffile)
                                         delete 'r*' 
                                         delete 'mean*' 
end %end of switch                                        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%  coregistration  %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
if coregistration
        s=spm_get('Files',[cwd,'T1Img/',subExpID(sub).name,'/'],'*.img'); % structural images from DICOM
        m=spm_get('Files',[cwd,'FunImgRAF/',subExpID(sub).name,'/'],'mean*.img'); %mean
        jobs{1}.spatial{1}.coreg{1}.estimate.ref = cellstr(m);       %set mean as the refernce
        jobs{1}.spatial{1}.coreg{1}.estimate.source = cellstr(s);     % structual image is coregisted to . Mean generated from realignment remains static
        spm_jobman('run',jobs);
        clear jobs;    

end %end of switch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%  segmentation  %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
if segmentation
     jobs{1}.spatial{1}.preproc.data=cellstr(s);  % segment gray/white matter of the structual images
     spm_jobman('run',jobs);
     clear jobs;
end %end of switch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%  normalization  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
if normalization

      s=spm_get('Files',[cwd,'T1Img/',subExpID(sub).name,'/'],'s*.img'); % structural images from DICOM
      m=spm_get('Files',[cwd,'FunImgRAF/',subExpID(sub).name,'/'],'mean*.img'); %mean


        %%%%%%%%%%%%%%%% normalization of functional images %%%%%%%%%%%%%%%%%%%%%
       sp=spm_get('Files',[ cwd,'T1Img/',subExpID(sub).name,'/'],'*seg_sn.mat') %parameters of structual images
       sm=spm_get('Files',[ cwd,'T1Img/',subExpID(sub).name,'/'],'m*.img') %path of structual images

       jobs{1}.spatial{1}.normalise{1}.write.subj.matname  = cellstr(sp);  %write parameters
       ff = spm_get('Files',[ cwd,'FunImgRAF/',subExpID(sub).name,'/'],'mean*.img') %mean 

        conCat_fs=[];  %set an empty matrix to contain all functional images
           for ses=1:nses
              dir=[cwd,'FunImgRAF/',subExpID(sub).name,'/']; %the location of motion correction file
              f=spm_get('Files',dir,'raf*.img') %motion correted files
              fs=cellstr(f);
              conCat_fs=[conCat_fs;fs]; %concatenate all files
              f = []; fs = [];  %clear
           end
       conCat_fs=[conCat_fs;cellstr(ff)]; %normalize mean
       jobs{1}.spatial{1}.normalise{1}.write.subj.resample = conCat_fs;   %start
       jobs{1}.spatial{1}.normalise{1}.write.roptions.bb = [-90 -126 -72
                                                            90 90 108];    % bouding box was [-78 -112 -50,78 76 85]. It is suggested that we set it as [-90 -126 -72,90 90 108] or [-78 -112 -70,78 76 85]
       jobs{1}.spatial{1}.normalise{1}.write.roptions.vox  = [3 3 3];  % set the size of voxel, close to collected


        %%%%%%%%%%%%%%% normalization of structual images %%%%%%%%%%%%%%%%%
         jobs{1}.spatial{1}.normalise{2}.write.subj.matname  = cellstr(sp)
         jobs{1}.spatial{1}.normalise{2}.write.subj.resample = cellstr(sm)
         jobs{1}.spatial{1}.normalise{2}.write.roptions.vox  = [1 1 1];     % set as it is collected
         spm_jobman('run',jobs);
         clear jobs;


                                          diroffile=[cwd,'FunImgRAF/',subExpID(sub).name,'/']   %add / in the end
                                          files=fullfile(diroffile,'wraf*')
                                          diroffile2=[cwd,'FunImgWRAF/',subExpID(sub).name,'/']
                                          copyfile(files,diroffile2)    %copy files
                                          cd(diroffile)
                                          delete 'wraf*' 

end %end of switch

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%  smoothing  %%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                       

if  smoothing    

        dirn=[cwd,'FunImgWRAF/',subExpID(sub).name,'/']; %location of motion corrected/normalized files
        fn=spm_get('Files',dirn,'wraf*.img') %motion corrected/normalized files
        jobs{1}.spatial{1}.smooth.data = cellstr(fn);    %smooth
        jobs{1}.spatial{1}.smooth.fwhm=[8 8 8]; % 平滑核大小,可以随意设置。default is 8

       spm_jobman('run',jobs);
       clear jobs;  


                                          diroffile=[cwd,'FunImgWRAF/',subExpID(sub).name,'/']   % add / int the end
                                          files=fullfile(diroffile,'swraf*')
                                          diroffile2=[cwd,'FunImgSWRAF/',subExpID(sub).name,'/']
                                          copyfile(files,diroffile2)     %copy
                                          cd(diroffile)
                                          delete 'swraf*' 
        %%%%%%%%%%%%%copy motion correction files to the last step %%%%%%%%%%%%%%%%

                                          diroffile=[cwd,'FunImgRAF/',subExpID(sub).name,'/']   %add / in the end
                                          files=fullfile(diroffile,'rp*.txt')
                                          diroffile2=[cwd,'FunImgSWRAF/',subExpID(sub).name,'/']
                                          copyfile(files,diroffile2)     %copy
end %end of switch 


%以下是使用DARTEL来进行标准化,这一步骤应该和DPARSF的相关功能类似。如果使用这一步,前面的转化,配准,分割,标准化,平滑就不用做了。但需要注意的是,这个过程耗时非常长。每个被试的DARTEL分析可能在1个小时左右。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% DARTEL normalization and smoothing  %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if DARTEL_NormSmooth
      %%%%%%%%%%%%%%% converting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         dirStruc=[cwd,'T1Raw/',subExpID(sub).name,'/']; %location of raw DICOM                 
         cd([ cwd,'DARTELT1Img/',subExpID(sub).name,'/']);  %location to export the Analyze.为了方面看结果,把DARTEL的结果都输出到一个单独的文件夹DARTELT1Img中,这里做转化结构像。最前面的转化结构像就不用做了,只转化功能像就行。
         ds=spm_get('Files',dirStruc,'*.ima') %list all DICOM file names
         hdrs=spm_dicom_headers(ds);     %read DICOM header   
         spm_dicom_convert(hdrs,'all','flat','img');     % convert
         display(sprintf('Anatomical run has been finished.'));     % show when finished
        clear dir;
        imgfile=dir ('*.img');     
        hdrfile=dir ('*.hdr'); 

        movefile(imgfile(1).name,[subExpID(sub).name,'.img'])  %change name

        movefile(hdrfile(1).name,[subExpID(sub).name,'.hdr'])  %change name 

    %%%%%%%%%%%%%%% coregistration %%%%%%%%%%%%%%%%%%%%%%%%%%%%
        s=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'*.img'); % structural images from DICOM
        m=spm_get('Files',[cwd,'FunImgRAF/',subExpID(sub).name,'/'],'mean*.img'); %mean
        jobs{1}.spatial{1}.coreg{1}.estimate.ref = cellstr(m);       %set mean as the reference
        jobs{1}.spatial{1}.coreg{1}.estimate.source = cellstr(s);     % structual image is coregisted to mean.  mean generated from realignment remains static
        spm_jobman('run',jobs);.
        clear jobs;    
    %%%%%%%%%%%%%%%%%% new segmentation %%%%%%%%%%%%%%%%%%%%%%%% 
        s=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'*.img'); % structural images from DICOM

            matlabbatch{1}.spm.tools.preproc8.channel.vols = cellstr(s);

            matlabbatch{1}.spm.tools.preproc8.channel.biasreg = 0.0001;
            matlabbatch{1}.spm.tools.preproc8.channel.biasfwhm = 60;
            matlabbatch{1}.spm.tools.preproc8.channel.write = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(1).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,1'};
            matlabbatch{1}.spm.tools.preproc8.tissue(1).ngaus = 2;
            matlabbatch{1}.spm.tools.preproc8.tissue(1).native = [1 1];
            matlabbatch{1}.spm.tools.preproc8.tissue(1).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(2).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,2'};
            matlabbatch{1}.spm.tools.preproc8.tissue(2).ngaus = 2;
            matlabbatch{1}.spm.tools.preproc8.tissue(2).native = [1 1];
            matlabbatch{1}.spm.tools.preproc8.tissue(2).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(3).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,3'};
            matlabbatch{1}.spm.tools.preproc8.tissue(3).ngaus = 2;
            matlabbatch{1}.spm.tools.preproc8.tissue(3).native = [1 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(3).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(4).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,4'};
            matlabbatch{1}.spm.tools.preproc8.tissue(4).ngaus = 3;
            matlabbatch{1}.spm.tools.preproc8.tissue(4).native = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(4).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(5).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,5'};
            matlabbatch{1}.spm.tools.preproc8.tissue(5).ngaus = 4;
            matlabbatch{1}.spm.tools.preproc8.tissue(5).native = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(5).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(6).tpm = {'/mnt/nfs/proj/addiction/spm8/toolbox/Seg/TPM.nii,6'};
            matlabbatch{1}.spm.tools.preproc8.tissue(6).ngaus = 2;
            matlabbatch{1}.spm.tools.preproc8.tissue(6).native = [0 0];
            matlabbatch{1}.spm.tools.preproc8.tissue(6).warped = [0 0];
            matlabbatch{1}.spm.tools.preproc8.warp.mrf = 0;
            matlabbatch{1}.spm.tools.preproc8.warp.reg = 4;
            matlabbatch{1}.spm.tools.preproc8.warp.affreg = 'mni';
            matlabbatch{1}.spm.tools.preproc8.warp.samp = 3;
            matlabbatch{1}.spm.tools.preproc8.warp.write = [0 0]; 

           spm_jobman('run',matlabbatch)  
           clear matlabbatch; 
%%%%%%%%%%%%%%%%%%vgeneration of templates %%%%%%%%%%%%%%%%%%%%%%%%%  
    rc1=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'rc1*.nii');  
    rc2=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'rc2*.nii');  

            matlabbatch{1}.spm.tools.dartel.warp.images = {cellstr(rc1),cellstr(rc2)};
            matlabbatch{1}.spm.tools.dartel.warp.settings.template = 'Template';
            matlabbatch{1}.spm.tools.dartel.warp.settings.rform = 0;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(1).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(1).rparam = [4 2 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(1).K = 0;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(1).slam = 16;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(2).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(2).rparam = [2 1 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(2).K = 0;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(2).slam = 8;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(3).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(3).rparam = [1 0.5 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(3).K = 1;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(3).slam = 4;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(4).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(4).rparam = [0.5 0.25 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(4).K = 2;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(4).slam = 2;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(5).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(5).rparam = [0.25 0.125 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(5).K = 4;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(5).slam = 1;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(6).its = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(6).rparam = [0.25 0.125 1e-06];
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(6).K = 6;
            matlabbatch{1}.spm.tools.dartel.warp.settings.param(6).slam = 0.5;
            matlabbatch{1}.spm.tools.dartel.warp.settings.optim.lmreg = 0.01;
            matlabbatch{1}.spm.tools.dartel.warp.settings.optim.cyc = 3;
            matlabbatch{1}.spm.tools.dartel.warp.settings.optim.its = 3;

            spm_jobman('run',matlabbatch)
            clear matlabbatch; 

%%%%%%%%% normalization and smoothing 使用DARTEL模板进行标准化%%%%%%%%%%%%%%%%%

        conCat_fs=[];  %set an empty matrix to contain all functional images
           for ses=1:nses
              dir=[cwd,'FunImgRAF/',subExpID(sub).name,'/']; %the location of motion corrected file
              f=spm_get('Files',dir,'raf*.img') %motion correted files
              fs=cellstr(f);
              conCat_fs=[conCat_fs;fs]; %concatenate all files
              f = []; fs = [];  %clear
           end

           flowfile=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'u_rc1*.nii'); 
           Templatefile=spm_get('Files',[cwd,'DARTELT1Img/',subExpID(sub).name,'/'],'Template_6.nii');

              matlabbatch{1}.spm.tools.dartel.mni_norm.template =cellstr(Templatefile);
              matlabbatch{1}.spm.tools.dartel.mni_norm.data.subj.flowfield =cellstr(flowfile);

              matlabbatch{1}.spm.tools.dartel.mni_norm.data.subj.images =conCat_fs; 

              matlabbatch{1}.spm.tools.dartel.mni_norm.vox = [3 3 3];
              matlabbatch{1}.spm.tools.dartel.mni_norm.bb = [-90 -126 -72
                                                            90 90 108];
              matlabbatch{1}.spm.tools.dartel.mni_norm.preserve = 0;
              matlabbatch{1}.spm.tools.dartel.mni_norm.fwhm = [8 8 8];


            spm_jobman('run',matlabbatch)


                                          diroffile=[cwd,'FunImgRAF/',subExpID(sub).name,'/']   % ad d / int the end
                                          files=fullfile(diroffile,'s*')
                                          diroffile2=[cwd,'DARTEL_FunImgSWRAF/',subExpID(sub).name,'/']
                                          copyfile(files,diroffile2)     %copy
                                          cd(diroffile)
                                          delete 'swraf*' 
            clear matlabbatch; 

%%%%%%%%%%%%%copy motion correction files to the last step %%%%%%%%%%%%%%%%

                                          diroffile=[cwd,'FunImgRAF/',subExpID(sub).name,'/']   %add / in the end
                                          files=fullfile(diroffile,'rp*.txt')
                                          diroffile2=[cwd,'DARTEL_FunImgSWRAF/',subExpID(sub).name,'/']
                                          copyfile(files,diroffile2)     %copy

end   % end of switch                               

end   % end of sub

Last updated