spm操作:
第一步:准备步骤
第二步:编辑Factorial design specification
2.1 选择保存生成结果的路径
2.2 选择分析的方法
2.3 选择一阶分析生成的图像文件(需要与行为结果做回归分析的文件)
选择一个被试:
选择所有被试:
2.4 增加协变量(行为数据)
4:按照nii顺序输入相应行为数据
7:给本次回归分析起个名字
2.5 其余保持默认即可,最后如下
第三步:编辑Model estimation
对上一步生成的SPM.mat文件进行估计
第四步:编辑Contrast Manager
4.1 选择上一步生成的SPM.mat文件
4.2 新建两个T-contrast
2:点击两下
4.3 编辑第一个T-contrast(positive)
4.4 编辑第二个T-contrast(negative)
第五步:运行batch
】
生成的文件
第六步:保存batch和script,稍作更改后可以用于批处理
批处理脚本:
第一步:文件路径框架
最上层路径。2:回归分析的结果,只要在这个路径新建一个 data_08_MultipleRegression 文件夹即可
1:一阶分析的结果具体路径;
第二步:将行为数据的excel文本保存在data_08_MultipleRegression/M5中
第三步:批处理脚本
生成batch
clc;
clear;
nmodel = '5';
numsub = 27;
rootdir = 'D:\LLYdata\motor_inhibition_data2\motor_inhibition_fmri';%数据最上层路径
ffxname = 'data_05_1st_level2';%1st分析文件名
rfxname = 'data_08_MultipleRegression';%2st分析文件名
batchfilename = 'batchfiles';%batchfiles文件名
ffxdir = fullfile(rootdir,ffxname,strcat('M',nmodel));%1st分析完整路径
rfxdir = fullfile(rootdir,rfxname,strcat('M',nmodel));%2st分析完整路径
%% 读保存在excel中的行为数据,并定义每个行为数据所在的列数
[~,~,data] = xlsread(fullfile(rfxdir,'behaviordata.xlsx'));
for i = 1:size(data,2)
eval([data{1,i},'=i']);
end
%%
merge_con = {...
'CGO-null','con_0001',{'RTCGoAll','TT2CGoAll','ETCGoAll','ITCGoAll'};
'CSL-null','con_0003',{'RTCGoL','TT2CGoL','ETCGoL','ITCGoL'};
'CSR-null','con_0005',{'RTCGoR','TT2CGoR','ETCGoR','ITCGoR'};
'UCGO-CGO','con_0028',{'RTUncGo','PreparationCostperSub'};
'UCSAS-UCGO','con_0040',{'SSRTStopAll','PreparationCostStopAllSuccess'};
'UCSAF-UCGO','con_0042',{'SSRTStopAll','RTUncSAFailed','PreparationCostStopAllFailed'};
'UCSAS-UCSAF','con_0059',{'SSRTStopAll'};
'(UCSLS-UCGO)-(CSL-CGO)','con_0067',{'RTUncSLSuccess','interferenceStopLeft','SSRTStopLeft','PreparationCostStopLeftSuccess'};
...
};%nii文件的名字,用于后续保存结果的文件命名;与con对应的nii文件编号;回归的行为数据
a = 0;
for ncon = 1:size(merge_con,1)
numbh = size(merge_con{ncon,3},2);
for nb = 1:numbh
a = a+1;
unfold_con{a,1} = merge_con{ncon,1};
unfold_con{a,2} = merge_con{ncon,2};
unfold_con{a,3} = merge_con{ncon,3}{1,nb};
end
end
for ncon = 1:size(unfold_con,1)
% 构建完整文件路径
for nsub = 1:numsub
full = fullfile(ffxdir,sprintf('%02d',nsub),strcat(unfold_con{ncon,2},'.nii,1'));
conpath{ncon}{nsub,1} = full;
end
%读取行为数据
bhdata{ncon} = cell2mat(data(2:end,eval(unfold_con{ncon,3})));%数据格式为一列
%命名
conname{ncon} = strcat(unfold_con{ncon,1},'&',unfold_con{ncon,3});
end
exmask='E:\MATLAB\toolbox\spm12\tpm\mask_ICV.nii';%mask的路径
%%
jobfile = [];
spm('defaults','fmri')
for ncon = 1:size(unfold_con,1)
clear matlabbatch
%%%生成保存结果的路径
if ~exist(fullfile(rfxdir,conname{ncon}),'dir')
mkdir(fullfile(rfxdir,conname{ncon}));
end
%%
matlabbatch{1}.spm.stats.factorial_design.dir = {fullfile(rfxdir,conname{ncon})};%保存结果的路径
matlabbatch{1}.spm.stats.factorial_design.des.mreg.scans = conpath{ncon};%需要与行为结果做回归分析的nii文件
matlabbatch{1}.spm.stats.factorial_design.des.mreg.mcov.c = bhdata{ncon};%行为结果
matlabbatch{1}.spm.stats.factorial_design.des.mreg.mcov.cname = conname{ncon};%命名
matlabbatch{1}.spm.stats.factorial_design.des.mreg.mcov.iCC = 1;
matlabbatch{1}.spm.stats.factorial_design.des.mreg.incint = 1;
matlabbatch{1}.spm.stats.factorial_design.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});
matlabbatch{1}.spm.stats.factorial_design.multi_cov = struct('files', {}, 'iCFI', {}, 'iCC', {});
matlabbatch{1}.spm.stats.factorial_design.masking.tm.tm_none = 1;
matlabbatch{1}.spm.stats.factorial_design.masking.im = 1;
matlabbatch{1}.spm.stats.factorial_design.masking.em = {exmask};
matlabbatch{1}.spm.stats.factorial_design.globalc.g_omit = 1;
matlabbatch{1}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;
matlabbatch{1}.spm.stats.factorial_design.globalm.glonorm = 1;
matlabbatch{2}.spm.stats.fmri_est.spmmat = {fullfile(rfxdir,conname{ncon},'SPM.mat')};
matlabbatch{2}.spm.stats.fmri_est.write_residuals = 0;
matlabbatch{2}.spm.stats.fmri_est.method.Classical = 1;
matlabbatch{3}.spm.stats.con.spmmat(1) = cfg_dep('Model estimation: SPM.mat File', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
matlabbatch{3}.spm.stats.con.consess{1}.tcon.name = 'positive';
matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [0 1];
matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'none';
matlabbatch{3}.spm.stats.con.consess{2}.tcon.name = 'negative';
matlabbatch{3}.spm.stats.con.consess{2}.tcon.weights = [0 -1];
matlabbatch{3}.spm.stats.con.consess{2}.tcon.sessrep = 'none';
matlabbatch{3}.spm.stats.con.delete = 0;
%% Save new job file
%%%生成保存batch的路径
batchfilesdir = fullfile(rfxdir,batchfilename);
if ~exist(batchfilesdir,'dir')
mkdir(batchfilesdir);
end
batchfile = conname{ncon};
save(fullfile(batchfilesdir,batchfile),'matlabbatch');
jobfile{end+1,1} = fullfile(batchfilesdir,batchfile);
fprintf('Saved batch file %s\n',batchfile)%命令行输出文字反馈
end
spm('chmod','fmri');
spm('defaults','fmri');
第四步:运行脚本后生成的文件
1:批处理脚本生成的batch所保存的位置
2:运行batch后文件保存的位置
第五步:运行batch
最后生成的结果文件
结束!打开result看结果就可以了~