关于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)
错误中文含义:
错误使用 mat2文件
索引超出了矩阵维度。
在文件“D:\Matlab\spm12@file_array\subsasgn.m”(v7147),第164行的函数“subfun”。
在文件“D:\Matlab\spm12@file_array\subsasgn.m”(v7147),第85行的函数“subsasgn”。
在文件“D:\Matlab\spm2\spm_write_plane.m”(v6079)的第31行执行函数“spm_write_prane”。
在文件“D:\Matlab\spm2\spm_slice_ttiming.m”(v6130)的第236行执行函数“spm_slica_ttiming”。
在文件“D:\Matlab\spm2\config\spm_run_st.m”(v4479)的第30行,函数“spm_run_st”。
没有可执行模块,但仍然存在未解析的依赖项或不完整的模块输入。
以下模块未运行:
失败:切片计时
已跳过:重新对齐:估计和重新切片
已跳过:核心注册表:估计
跳过:段
已跳过:规范化:写入
跳过:平滑
错误使用 MATLABbatch系统
作业执行失败。该运行的完整日志可以在MATLAB命令窗口中找到,从行开始(查找显示确切#job的行
显示在此错误消息中)
从中文释义分析,该错误表明在使用 SPM12 进行批量预处理时,出现了一个错误。该错误是 mat2file 函数的调用,该函数在将矩阵写入文件时超出了矩阵的维度。
该错误还指出了一些其他函数和模块,它们在调用 mat2file 函数时也出现了问题。这些问题导致了一些模块无法运行,例如 Slice Timing 和 Realign: Estimate & Reslice。
建议:
1、首先,检查导入的文件路径和文件名。最好不要有空格、中文等其他特殊字符
2、其次,检查您的数据文件是否完整,有没有未安装或者重复的依赖项
3、检查代码正确地调用了 mat2file 函数,并且没有超出矩阵的维度
文件路径问题:确保在循环中正确设置每个被试的文件路径。请检查parentdir变量的路径是否正确,并确保该路径包含每个被试的文件夹。还要确保每个被试文件夹中的anat、tfMRI1和tfMRI2文件夹存在。
很遗憾,您提供的代码没有执行完,无法判断具体错误所在。请您提供完整的代码以及产生的错误信息,这样才能更好地帮助您解决问题。
SPM12 核磁数据预处理
可以借鉴下
https://blog.csdn.net/Iris_bysshqx17/article/details/100188483
首先确认MATLAB或SPM12的版本问题,其次你数据的数量是否超过了SPM12的处理限制,还有你的文件损坏或格式不正确
错误使用mat2file index exceeds matrix这个错误看着像是数组下标越界导致的,检查下你的matlab代码是否有使用到数组和下标索引的地方。其次错误中还有一个依赖的错误,看下是不是有些依赖没有导入正确
可能一些数据的尺寸或者维度设置有误。检查数据格式、维度是否正确,如果要处理多个数据文件,需要保证每个数据文件之间的维度和间隔等参数尽量一致。如果数据不一致,可能会导致 Slice Timing 等模块运行失败。