关于spm12的batch批处理问题,望解答
在使用spm12进行批量预处理碰到了问题,一直报错,但使用spm12的窗口里面的batch处理单个被试能正常运行
%
% 进入数据文件夹
parentdir = 'E:\study\analysis\data\HYP'; % 定义储存各被试源文件的上级文件夹
cd(parentdir); % 进入这个上级文件夹
allsubjects = dir('s*');%查找该文件夹下的所有被试
numsubjects = numel(allsubjects); % 数找到了多少被试
for i=1:2 % 对每个被试进行循环
cursubject = allsubjects(i).name; % 找到当前被试的名字
matlabbatch=cell(1);
%% 查找结构像
% 查找第i个被试的anat文件夹的绝对路径
curanat = fullfile(parentdir,cursubject,'anat');
% 查找第i个被试的anat下的所有dcm文件
alldcms = dir(fullfile(curanat,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curanat,{alldcms.name}');
matlabbatch{1}.spm.util.import.dicom.data = alldcms; % 把刚刚结构像的文件告诉给matlabbatch{1}.spm.util.import.dicom.data
% 接下来指定anat的输出文件夹
curanatout=fullfile('E:\study\analysis\output\HYP',cursubject,'anat');
if ~exist(curanatout,'dir')
mkdir(curanatout);
end
%%
matlabbatch{1}.spm.util.import.dicom.root = 'flat';
matlabbatch{1}.spm.util.import.dicom.outdir = {curanatout};
matlabbatch{1}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{1}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{1}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{1}.spm.util.import.dicom.convopts.icedims = 0;
%% 查找flanker1
% 查找第i个被试的flanker1文件夹的绝对路径
curtfMRI1 = fullfile(parentdir,cursubject,'tfMRI1');
% 查找第i个被试的anat下的所有dcm文件
alldcms = dir(fullfile(curtfMRI1,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curtfMRI1,{alldcms.name}');
matlabbatch{2}.spm.util.import.dicom.data = alldcms(5:end,1); % 把功能像的前面4个文件去掉,以控制匀场效应;同时把这些文件告诉给matlab
%%
% 接下来指定flanker1的输出文件夹
curfucout=fullfile('E:\study\analysis\output\HYP',cursubject,'tfMRI1');
if ~exist(curfucout,'dir')
mkdir(curfucout);
end
matlabbatch{2}.spm.util.import.dicom.root = 'flat';
matlabbatch{2}.spm.util.import.dicom.outdir = {curfucout};
matlabbatch{2}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{2}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{2}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{2}.spm.util.import.dicom.convopts.icedims = 0;
%%
%% 查找flanker2
% 查找第i个被试的flanker2文件夹的绝对路径
curtfMRI2 = fullfile(parentdir,cursubject,'tfMRI2');
% 查找第i个被试的flanker2下的所有dcm文件
alldcms = dir(fullfile(curtfMRI2,'*.dcm'));
% 再把alldcms中每个文件添加上绝对路径,这样才能让spm的batch找到文件
alldcms = fullfile(curtfMRI2,{alldcms.name}');
matlabbatch{3}.spm.util.import.dicom.data = alldcms(5:end,1);
%% 接下来指定flanker2的输出文件夹
curfucout=fullfile('E:\study\analysis\output\HYP',cursubject,'tfMRI2');
if ~exist(curfucout,'dir')
mkdir(curfucout);
end
matlabbatch{3}.spm.util.import.dicom.root = 'flat';
matlabbatch{3}.spm.util.import.dicom.outdir = {curfucout};
matlabbatch{3}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{3}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{3}.spm.util.import.dicom.convopts.meta = 0;
matlabbatch{3}.spm.util.import.dicom.convopts.icedims = 0;
matlabbatch{4}.spm.temporal.st.scans{1}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{4}.spm.temporal.st.scans{2}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{4}.spm.temporal.st.nslices = 33;
matlabbatch{4}.spm.temporal.st.tr = 2;
matlabbatch{4}.spm.temporal.st.ta = 1.93939393939394;
matlabbatch{4}.spm.temporal.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];
matlabbatch{4}.spm.temporal.st.refslice = 33;
matlabbatch{4}.spm.temporal.st.prefix = 'a';
matlabbatch{5}.spm.spatial.realign.estwrite.data{1}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 1)', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{5}.spm.spatial.realign.estwrite.data{2}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 2)', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{2}, '.','files'));
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.interp = 2;
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{5}.spm.spatial.realign.estwrite.eoptions.weight = '';
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.which = [2 1];
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{5}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
matlabbatch{6}.spm.spatial.coreg.estimate.ref(1) = cfg_dep('Realign: Estimate & Reslice: Mean Image', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean'));
matlabbatch{6}.spm.spatial.coreg.estimate.source(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{6}.spm.spatial.coreg.estimate.other = {''};
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.tol = [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];
matlabbatch{6}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
matlabbatch{7}.spm.spatial.preproc.channel.vols(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles'));
matlabbatch{7}.spm.spatial.preproc.channel.biasreg = 0.001;
matlabbatch{7}.spm.spatial.preproc.channel.biasfwhm = 60;
matlabbatch{7}.spm.spatial.preproc.channel.write = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(1).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,1'}; %%使用ctrl+h替换成自己的spm的位置,范围为spm12前的文件名
matlabbatch{7}.spm.spatial.preproc.tissue(1).ngaus = 1;
matlabbatch{7}.spm.spatial.preproc.tissue(1).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(1).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(2).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,2'};
matlabbatch{7}.spm.spatial.preproc.tissue(2).ngaus = 1;
matlabbatch{7}.spm.spatial.preproc.tissue(2).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(2).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(3).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,3'};
matlabbatch{7}.spm.spatial.preproc.tissue(3).ngaus = 2;
matlabbatch{7}.spm.spatial.preproc.tissue(3).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(3).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(4).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,4'};
matlabbatch{7}.spm.spatial.preproc.tissue(4).ngaus = 3;
matlabbatch{7}.spm.spatial.preproc.tissue(4).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(4).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(5).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,5'};
matlabbatch{7}.spm.spatial.preproc.tissue(5).ngaus = 4;
matlabbatch{7}.spm.spatial.preproc.tissue(5).native = [1 0];
matlabbatch{7}.spm.spatial.preproc.tissue(5).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(6).tpm = {'D:\Matlab\spm12\tpm\TPM.nii,6'};
matlabbatch{7}.spm.spatial.preproc.tissue(6).ngaus = 2;
matlabbatch{7}.spm.spatial.preproc.tissue(6).native = [0 0];
matlabbatch{7}.spm.spatial.preproc.tissue(6).warped = [0 0];
matlabbatch{7}.spm.spatial.preproc.warp.mrf = 1;
matlabbatch{7}.spm.spatial.preproc.warp.cleanup = 1;
matlabbatch{7}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
matlabbatch{7}.spm.spatial.preproc.warp.affreg = 'mni';
matlabbatch{7}.spm.spatial.preproc.warp.fwhm = 0;
matlabbatch{7}.spm.spatial.preproc.warp.samp = 3;
matlabbatch{7}.spm.spatial.preproc.warp.write = [1 1];
matlabbatch{7}.spm.spatial.preproc.warp.vox = NaN;
matlabbatch{7}.spm.spatial.preproc.warp.bb = [NaN NaN NaN
NaN NaN NaN];
matlabbatch{8}.spm.spatial.normalise.write.subj.def(1) = cfg_dep('Segment: Forward Deformations', substruct('.','val', '{}',{7}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','fordef', '()',{':'}));
matlabbatch{8}.spm.spatial.normalise.write.subj.resample(1) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 1)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{1}, '.','rfiles'));
matlabbatch{8}.spm.spatial.normalise.write.subj.resample(2) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 2)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{2}, '.','rfiles'));
matlabbatch{8}.spm.spatial.normalise.write.woptions.bb = [-90 -126 -72
90 90 108];
matlabbatch{8}.spm.spatial.normalise.write.woptions.vox = [3 3 3];
matlabbatch{8}.spm.spatial.normalise.write.woptions.interp = 4;
matlabbatch{8}.spm.spatial.normalise.write.woptions.prefix = 'w';
matlabbatch{9}.spm.spatial.smooth.data(1) = cfg_dep('Normalise: Write: Normalised Images (Subj 1)', substruct('.','val', '{}',{8}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{9}.spm.spatial.smooth.fwhm = [8 8 8];
matlabbatch{9}.spm.spatial.smooth.dtype = 0;
matlabbatch{9}.spm.spatial.smooth.im = 0;
matlabbatch{9}.spm.spatial.smooth.prefix = 's';
save(fullfile(parentdir,cursubject,'batch.mat'),'matlabbatch');
spm_jobman('run',matlabbatch);
end
报错内容:
错误使用 mat2file
Index exceeds matrix dimensions.
In file "D:\Matlab\spm12@file_array\subsasgn.m" (v7147), function "subfun" at line 164.
In file "D:\Matlab\spm12@file_array\subsasgn.m" (v7147), function "subsasgn" at line 85.
In file "D:\Matlab\spm12\spm_write_plane.m" (v6079), function "spm_write_plane" at line 31.
In file "D:\Matlab\spm12\spm_slice_timing.m" (v6130), function "spm_slice_timing" at line 236.
In file "D:\Matlab\spm12\config\spm_run_st.m" (v4479), function "spm_run_st" at line 30.
No executable modules, but still unresolved dependencies or incomplete module inputs.
The following modules did not run:
Failed: Slice Timing
Skipped: Realign: Estimate & Reslice
Skipped: Coregister: Estimate
Skipped: Segment
Skipped: Normalise: Write
Skipped: Smooth
错误使用 MATLABbatch system
Job execution failed. The full log of this run can be found in MATLAB command window, starting with the lines (look for the line showing the exact #job as
displayed in this error message)