SPM操作:
9-SPM-fmri任务态 二阶分析: 双样本t检验 - 知乎 (zhihu.com)
批处理脚本:
clc;
clear;
nmodel = '3';
numsub = 27;
rootdir = 'D:\LLYdata\motor_inhibition_data2\motor_inhibition_fmri';%数据最上层路径
ffxname = 'data_05_1st_level2';%1st分析文件名
rfxname = 'data_09_independentTtest';%2st分析文件名
batchfilename = 'batchfiles';%batchfiles文件名
ffxdir = fullfile(rootdir,ffxname,strcat('M',nmodel));%1st分析完整路径
rfxdir = fullfile(rootdir,rfxname,strcat('M',nmodel));%2st分析完整路径
load(fullfile(rfxdir,'conpos.mat'));
load(fullfile(rfxdir,'conneg.mat'));
%%
group1 = {'02';'04';'05';'10';'12';'13';'14';'15';'18';'24';'25';'26';'27'};%低成本
group2 = {'01';'03';'06';'07';'08';'09';'11';'16';'17';'19';'20';'21';'22';'23'};%高成本
%% 生成conname
%%%删除其中的*
for i = 1:size(conpos,2)
if contains(conpos(1,i), '*')
conpos(1,i) = strrep(conpos(1,i), '*', '');
end
if contains(conneg(1,i), '*')
conneg(1,i) = strrep(conneg(1,i), '*', '');
end
end
conname = cell(1,1);
for ncon = 1:size(conpos,2)
if ncon == 1
conname{end} = strcat(conpos{ncon},'-',conneg{ncon});
else
conname{end+1} = strcat(conpos{ncon},'-',conneg{ncon});
end
conname{end+1} = strcat(conneg{ncon},'-',conpos{ncon});
end
%%
jobfile = [];
spm('defaults','fmri')
for ncon = 1:2*size(conpos,2)
clear matlabbatch
%%%生成保存结果的路径
if ~exist(fullfile(rfxdir,conname{ncon}),'dir')
mkdir(fullfile(rfxdir,conname{ncon}));
end
%%%group data path
for nsub = 1:size(group1,1)
group1_path{nsub,1} = fullfile(ffxdir,group1{nsub},strcat(sprintf('con_%04d',ncon),'.nii,1'));
end
for nsub = 1:size(group2,1)
group2_path{nsub,1} = fullfile(ffxdir,group2{nsub},strcat(sprintf('con_%04d',ncon),'.nii,1'));
end
%%
matlabbatch{1}.spm.stats.factorial_design.dir = {fullfile(rfxdir,conname{ncon})};
matlabbatch{1}.spm.stats.factorial_design.des.t2.scans1 = group1_path;
%%
%%
matlabbatch{1}.spm.stats.factorial_design.des.t2.scans2 = group2_path;
%%
matlabbatch{1}.spm.stats.factorial_design.des.t2.dept = 0;
matlabbatch{1}.spm.stats.factorial_design.des.t2.variance = 1;
matlabbatch{1}.spm.stats.factorial_design.des.t2.gmsca = 0;
matlabbatch{1}.spm.stats.factorial_design.des.t2.ancova = 0;
%%
matlabbatch{1}.spm.stats.factorial_design.cov(1).c = [22
20
20
20
18
25
25
25
25
21
19
26
20
28
21
25
20
26
21
25
19
19
27
23
19
19
23
];
%%
matlabbatch{1}.spm.stats.factorial_design.cov(1).cname = 'age';
matlabbatch{1}.spm.stats.factorial_design.cov(1).iCFI = 1;
matlabbatch{1}.spm.stats.factorial_design.cov(1).iCC = 1;
%%
matlabbatch{1}.spm.stats.factorial_design.cov(2).c = [2
2
1
1
1
1
2
2
1
2
1
1
2
1
2
2
1
1
1
2
1
1
2
1
1
2
2
];
%%
matlabbatch{1}.spm.stats.factorial_design.cov(2).cname = 'sex';
matlabbatch{1}.spm.stats.factorial_design.cov(2).iCFI = 1;
matlabbatch{1}.spm.stats.factorial_design.cov(2).iCC = 1;
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 = {'E:\MATLAB\toolbox\spm12\tpm\mask_ICV.nii,1'};
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(1) = cfg_dep('Factorial design specification: SPM.mat File', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
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 = 'g1>g2';
matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [1 -1];
matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'none';
matlabbatch{3}.spm.stats.con.consess{2}.tcon.name = 'g1<g2';
matlabbatch{3}.spm.stats.con.consess{2}.tcon.weights = [-1 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');