function varargout = FASL(varargin); % % FASL v0.0.1 % by Steven M. Lulich % slulich@iu.edu % Speech Production Laboratory @ Indiana University % https://spliu.synology.me/software/software.html % % The "copyAndConvert" function of FASL v0.0.1 requires prior installation of the dicm2nii.m package % (https://www.mathworks.com/matlabcentral/fileexchange/42997-xiangruili-dicm2nii). % % The SPMBATCH and SPMLEVEL1 functions of FASL v0.0.1 require that SPM is installed and in the MATLAB path % (https://www.fil.ion.ucl.ac.uk/spm/) % % The TOPUP function of FASL v0.0.1 requires that FSL is installed and in the MATLAB path % (https://fsl.fmrib.ox.ac.uk/fsl/docs/index.html) % % Note that the version of FASL used in the Speech Production Laboratory @ Indiana University is not identical to v0.0.1 % The version in use in the lab automatically includes access to dicm2nii.m, SPM, and FSL. Users of v0.0.1 may need to % troubleshoot and edit the code in order to use it. % % % varargout = FASL(varargin); % % FASL; The ACTION defaults to 'doNothing', which falls in the % 'otherwise' category at the end of the switch % statement. % FASL(ACTION); The ACTION can be one of the following: % GUI, ALL, TOPUP, SPMBATCH, SPMLEVEL1 % Internal function calls also make use of ACTION values as followsl: % selectOriginDirectory, selectAnalysisDirectory, copyAndConvert, organize, selectAnatomicalReferenceImage, selectSeries % FASL('GUI'); The primary entry point to FASL - this launches the GUI. % FASL('ALL'); Runs TOPUP followed by SPMBATCH. % FASL('TOPUP'); Runs TOPUP processing on the current directory. % Current directory must already contain *.nii % files (e.g. following use of the dicm2nii.m package). % FASL('SPMBATCH'); Creates a batch file for SPM processing (e.g. % alignment, registration, smoothing, etc.). % FASL('SPMLEVEL1'); Creates a batch file for SPM Level 1 analysis, % including definitions of contrasts. Requires % the SPM processing batch file to have been run % in SPM already. % if nargin == 1, action = varargin{1}; elseif nargin == 4, f = varargin{3}; action = varargin{4}; else, action = 'doNothing'; end; pathname = pwd; switch action, case 'GUI', % Under Construction f = figure('Units','normalized','Position',[0.01 0.01 0.9 0.9],'Name','FASL v0.0.1','NumberTitle','off');%,'HandleVisibility','off','CloseRequestFcn',''); % Parameters that need to be determined: % - directory to be analyzed % - anatomical reference image % - series to be analyzed (e.g. "phonation") % - type of analysis (e.g. "fmri") % - steps in the SPM batch processing % - moving the data to a destination folder in the lab slate-project % - alignment % - registration % - reslicing % - smoothing % - normalization % -order of operations/steps (dicom2nii, FASL, SPM, FASL, SPM, % FSLeyes, ...) % - use the HRF or not? % PushButton and Edit (disabled) for the origin directory (optional) f.UserData.selectOriginDirectory = ... uicontrol('Parent',f,... 'style','pushbutton','Units','normalized','Position',[0.02 0.95 0.25 0.03],... 'String','Select Origin Directory (optional, for copying)','Callback',{@FASL,f,'selectOriginDirectory'}); f.UserData.originDirectory = ... uicontrol('Parent',f,... 'style','Edit','Units','normalized','Position',[0.02 0.9 0.25 0.03],... 'String','','Enable','off'); % PushButton and Edit (disabled) for the destination directory (or the directory to be analyzed) % PushButton for moving the data and running dicm2nii f.UserData.selectAnalysisDirectory = ... uicontrol('Parent',f,... 'style','pushbutton','Units','normalized','Position',[0.02 0.85 0.25 0.03],... 'String','Select Analysis Directory','Callback',{@FASL,f,'selectAnalysisDirectory'}); f.UserData.analysisDirectory = ... uicontrol('Parent',f,... 'style','Edit','Units','normalized','Position',[0.02 0.8 0.25 0.03],... 'String','','Enable','off'); % PushButton to copy files from origin directory to analysis % directory, to run dicm2nii.m. f.UserData.copyAndConvert = ... uicontrol('Parent',f,... 'style','pushbutton','Units','normalized','Position',[0.02 0.75 0.1 0.03],... 'String','Copy and Convert','Callback',{@FASL,f,'copyAndConvert'},... 'Visible','off','Enable','off'); % PushButton to organize files. f.UserData.organize = ... uicontrol('Parent',f,... 'style','pushbutton','Units','normalized','Position',[0.17 0.75 0.1 0.03],... 'String','Organize','Callback',{@FASL,f,'organize'},... 'Visible','off','Enable','off'); % PushButton and Edit (disabled) for anatomical reference image f.UserData.selectAnatomicalReferenceImage = ... uicontrol('Parent',f,... 'style','pushbutton','Units','normalized','Position',[0.02 0.7 0.25 0.03],... 'String','Select Anatomical Reference Image','Callback',{@FASL,f,'selectAnatomicalReferenceImage'},... 'Enable','off'); f.UserData.anatomicalReferenceImage = ... uicontrol('Parent',f,... 'style','Edit','Units','normalized','Position',[0.02 0.65 0.25 0.03],... 'String','','Enable','off'); % PushButton and Edit (disabled) for series to be analyzed f.UserData.selectSeries = ... uicontrol('Parent',f,... 'style','popupmenu','Units','normalized','Position',[0.02 0.6 0.25 0.03],... 'String',{'Select Series to Analyze'},'Callback',{@FASL,f,'selectSeries'},... 'Enable','off'); f.UserData.series = ... uicontrol('Parent',f,... 'style','Edit','Units','normalized','Position',[0.02 0.55 0.25 0.03],... 'String','','Enable','off'); % Drop-down menu for type of analysis UserData.selectAnalysis = ... uicontrol('Parent',f,... 'style','popupmenu','Units','normalized','Position',[0.02 0.1 0.25 0.03],... 'String',{'fMRI'},... 'Callback',{@FASL,f,'selectAnalysis'},'Enable','off'); % Two Listboxes, one with the set of possible steps, the other with the selected steps. PushButtons to move steps up or down in the selection Listbox. % A TabGroup with Tabs for each step in the selection ListBox % Each Tab asks relevant questions - including smoothing kernel size, and whether to reslice, what the alignment or registration images are referenced to, whether to use the HRF, etc. % PushButton for creating the batch processing file, opening SPM, running the process, and closing SPM. % % Set up the Level 1 analysis batch file % Define contrasts % PushButton for creating the batch file, opening SPM, running the process, and closing SPM. % % PushButton for opening FSLeyes in the target directory. % FYI according to Google AI Overview, the critical t-value can be % obtained in this way: % % load('SPM.mat'); % df = [SPM.xCon(con_idx).eidf, SPM.xX.erdf]; % R = RPM.xVOl.R % S = SPM.xVol.S % uc_fwe = spm_uc(0.05, df, 'T', R, 1, S); % % or u = spm_u(0.001, df ,'T'); % case 'selectOriginDirectory', f.UserData.originDirectory.String = uigetdir(pwd,'Select the directory with raw dicom data...'); f.UserData.copyAndConvert.Visible = 'on'; f.UserData.organize.Visible = 'on'; case 'selectAnalysisDirectory', if ~isempty(f.UserData.originDirectory.String), [a,directoryName,c] = fileparts(f.UserData.originDirectory.String); f.UserData.analysisDirectory.String = fullfile(uigetdir(pwd,'Select the parent directory for copying and analysis...'),directoryName); mkdir(f.UserData.analysisDirectory.String); f.UserData.copyAndConvert.Enable = 'on'; else, f.UserData.analysisDirectory.String = uigetdir(pwd,'Select the directory to analyze...'); f.UserData.selectAnatomicalReferenceImage.Enable = 'on'; end; case 'copyAndConvert', f.UserData.selectOriginDirectory.Enable = 'off'; f.UserData.selectAnalysisDirectory.Enable = 'off'; f.UserData.copyAndConvert.Enable = 'off'; f.UserData.copyAndConvert.String = 'Please wait...'; pause(0.1); [~,~,~] = mkdir(f.UserData.analysisDirectory.String); % Organize the files [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'func'); [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'fmap'); [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'corrected_func'); [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'anat'); [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'mfmri'); [~,~,~] = mkdir(f.UserData.analysisDirectory.String,'dcm'); d = dir(f.UserData.originDirectory.String); for n=3:numel(d), copyfile(fullfile(f.UserData.originDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'dcm',d(n).name)); end; dicm2nii(f.UserData.originDirectory.String, f.UserData.analysisDirectory.String, 0); f.UserData.selectOriginDirectory.Enable = 'on'; f.UserData.selectAnalysisDirectory.Enable = 'on'; f.UserData.copyAndConvert.Enable = 'on'; f.UserData.copyAndConvert.String = 'Copy and Convert'; f.UserData.organize.Enable = 'on'; f.UserData.selectAnatomicalReferenceImage.Enable = 'on'; case 'organize', f.UserData.copyAndConvert.Enable = 'off'; f.UserData.organize.Enable = 'off'; f.UserData.organize.String = 'Please wait...'; pause(0.1); d = dir(f.UserData.analysisDirectory.String); for n=3:numel(d), if contains(d(n).name,'nii') | contains(d(n).name,'json'), if contains(d(n).name,'mfMRI'), movefile(fullfile(f.UserData.analysisDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'mfmri',d(n).name)); elseif contains(d(n).name,'Spin') | contains(d(n).name,'fmap'), movefile(fullfile(f.UserData.analysisDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'fmap',d(n).name)); elseif contains(d(n).name,'func'), movefile(fullfile(f.UserData.analysisDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'func',d(n).name)); elseif contains(d(n).name,'T1w'), movefile(fullfile(f.UserData.analysisDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'anat',d(n).name)); elseif contains(d(n).name,'tfl'), movefile(fullfile(f.UserData.analysisDirectory.String,d(n).name),fullfile(f.UserData.analysisDirectory.String,'anat',d(n).name)); else % Leave it alone. end; end; end; %gunzip(fullfile(f.UserData.analysisDirectory.String,'func','*.gz')); %gunzip(fullfile(f.UserData.analysisDirectory.String,'anat','*.gz')); f.UserData.copyAndConvert.Enable = 'on'; f.UserData.organize.Enable = 'on'; f.UserData.organize.String = 'Organize'; case 'selectAnatomicalReferenceImage', f.UserData.anatomicalReferenceImage.String = fullfile(f.UserData.analysisDirectory.String,'anat',uigetfile(fullfile(f.UserData.analysisDirectory.String,'anat'),'Select an anatomical reference image...')); d = dir(fullfile(f.UserData.analysisDirectory.String,'func','*.nii')); list{1} = f.UserData.selectSeries.String{1}; m = 1; for n = 1:numel(d), if ~contains(d(n).name,'SBRef'), idx = strfind(d(n).name,'_'); seriesName = d(n).name(idx(2)+1:idx(end)-1); if ~sum(contains(list,seriesName)), list{m+1} = d(n).name(idx(2)+1:idx(end)-1); m=m+1; end; end; end; f.UserData.selectSeries.String = list; f.UserData.selectSeries.Enable = 'on'; case 'selectSeries', f.UserData.series.String = f.UserData.selectSeries.String{f.UserData.selectSeries.Value}; case 'ALL', FASL('TOPUP'); FASL('SPMBATCH'); case 'TOPUP', %s = input('Did you use dicm2nii to convert dcm files to nii? (y/n)'); %if s ~= 'y', % disp('Convert to nii and then run FASL(''TOPUP'') or FASL(''ALL'').'); % return; %end; %disp('Getting organized. Please wait...'); systemToggle = 1; %[~,~,~] = mkdir(pathname,'func'); %[~,~,~] = mkdir(pathname,'fmap'); %[~,~,~] = mkdir(pathname,'corrected_func'); %[~,~,~] = mkdir(pathname,'anat'); %[~,~,~] = mkdir(pathname,'mfmri'); %[~,~,~] = mkdir(pathname,'dcm'); %d = dir(pathname); %for n=3:numel(d), % if isfile(d(n).name), % if contains(d(n).name,'mfMRI'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'mfmri',d(n).name)); % elseif contains(d(n).name,'Spin'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'fmap',d(n).name)); % elseif contains(d(n).name,'func'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'func',d(n).name)); % elseif contains(d(n).name,'T1w'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'anat',d(n).name)); % elseif contains(d(n).name,'tfl'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'anat',d(n).name)); % elseif contains(d(n).name,'dcm'), % movefile(fullfile(pathname,d(n).name),fullfile(pathname,'dcm',d(n).name)); % else % % Leave it alone. % end; % end; %end; %gunzip(fullfile(pathname,'func','*.gz')); %gunzip(fullfile(pathname,'anat','*.gz')); disp('Processing TOPUP. Please wait...'); d = dir(fullfile(pathname,'fmap')); for n=1:numel(d), if ~isempty(strfind(d(n).name,'AP')) & ~isempty(strfind(d(n).name,'gz')), AP_filename = fullfile(pathname,'fmap',d(n).name); end; if ~isempty(strfind(d(n).name,'PA')) & ~isempty(strfind(d(n).name,'gz')), PA_filename = fullfile(pathname,'fmap',d(n).name); k = strfind(d(n).name,'PA'); filename = d(n).name; end; end; APPA_filename = fullfile(pathname,'fmap',[filename(1:k-1-4) 'APPA_combined' filename(k+3+3:end)]); if systemToggle, command = ['module load fsl']; system(command); command = ['fslmerge -t ' APPA_filename ' ' AP_filename ' ' PA_filename]; system(command); end; disp(' Done with APPA file.'); d = dir(fullfile(pathname,'fmap')); for n=1:numel(d), if ~isempty(strfind(d(n).name,'json')), totalReadoutTime = jsondecode(fileread(fullfile(pathname,'fmap',d(n).name))).TotalReadoutTime; %%%%%%%%%%%%%%%%%%%%%%%%%%% textline = ['0 -1 0 ' num2str(totalReadoutTime)]; fid = fopen(fullfile(pathname,'acqparams.txt'),'a'); for m=1:3, fprintf(fid,'%s\n',textline); end; textline = ['0 1 0 ' num2str(totalReadoutTime)]; for m=1:3, fprintf(fid,'%s\n',textline); end; fclose(fid); break; end; end; disp([' fmap totalReadoutTime is: ' num2str(totalReadoutTime)]); disp(' Done with acqparams.txt'); fieldcoef_filename = 'topup_results'; command = ['topup --imain=' APPA_filename ' --datain=' fullfile(pathname,'acqparams.txt') ' --config=b02b0.cnf --out=' fullfile(pathname,fieldcoef_filename) ' --iout=corrected_images']; if systemToggle, system(command); end; disp(' Done with fieldcoef file.'); d = dir(fullfile(pathname,'func')); %%% FIND OUT HOW MANY FRAMES (dim4) - ASSUME THE SAME FOR ALL RUNS for n=1:numel(d), if contains(d(n).name,'func') & contains(d(n).name,'nii.gz') & ~contains(d(n).name,'SBRef'), I = niftiinfo(fullfile(pathname,'func',d(n).name)); dim4 = I.ImageSize(4); %%%%%%%%%%%%%%%%%%%%%%%%%%% n = numel(d); % Assumes the same dim4 for every file in the directory end; end; disp([' Number of frames: ' num2str(dim4)]); %%% FOR EACH RUN, CREATE THE acqparams1.txt FILE WITH dim4 NUMBER %%% OF ROWS acqparams_filename = 'acqparams1.txt'; for n=1:numel(d), if ~isempty(strfind(d(n).name,'json')), totalReadoutTime = jsondecode(fileread(fullfile(pathname,'func',d(n).name))).TotalReadoutTime; %%%%%%%%%%%%%%%%%%%%%%%%%%% textline = ['0 -1 0 ' num2str(totalReadoutTime)]; fid = fopen(fullfile(pathname,acqparams_filename),'a'); for m=1:dim4, fprintf(fid,'%s\n',textline); end; fclose(fid); break; end; end; disp([' fmap totalReadoutTime is: ' num2str(totalReadoutTime)]); disp(' Done with acqparams1.txt'); %%% FOR EACH RUN, APPLYTOPUP WITH dim4 NUMBER OF ROWS N = 0; disp('Please wait...'); for n=1:numel(d), if contains(d(n).name,'func') & contains(d(n).name,'nii.gz'), N = N+1; disp([' Processing file #' num2str(N)]); command = ['applytopup --imain=' fullfile(pathname,'func',d(n).name) ' --datain=' fullfile(pathname,acqparams_filename) ' --topup=' fullfile(pathname,[fieldcoef_filename '_fieldcoef']) ' --out=' fullfile(pathname,'corrected_func',['corrected_' d(n).name]) ' --method=jac --inindex=1-' num2str(dim4)]; disp(command); if systemToggle, system(command); end; end; end; gunzip(fullfile(pathname,'corrected_func','*.gz')); disp('Done with TOPUP processing.'); case 'SPMBATCH', disp('Setting up SPM preprocessing batch file. Please wait...'); spm_jobman('initcfg'); task = 0; task = task + 1; d = dir(fullfile(pathname,'corrected_func')); idx = 1; for n=1:numel(d), if ~contains(d(n).name,'meancorrected') & ~contains(d(n).name,'swcorrected') & ~contains(d(n).name,'wcorrected') & contains(d(n).name,'corrected') & contains(d(n).name,'phonation') & ~contains(d(n).name,'gz') & ~contains(d(n).name,'SBRef') & ~contains(d(n).name,'mat') & ~contains(d(n).name,'txt'), info = niftiinfo(fullfile(pathname,'corrected_func',d(n).name)); if info.ImageSize(4) == 260, for k = 1:info.ImageSize(4), data{idx}{k,1} = fullfile(pathname,'corrected_func',[d(n).name ',' num2str(k)]); end; idx = idx + 1; end; end; end; matlabbatch{task}.spm.spatial.realign.estimate.data = data; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.quality = 0.9; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.sep = 4; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.fwhm = 5; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.rtm = 1; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.interp = 2; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.wrap = [0 0 0]; matlabbatch{task}.spm.spatial.realign.estimate.eoptions.weight = ''; task = task + 1; for n = 1:numel(matlabbatch{1}.spm.spatial.realign.estimate.data), matlabbatch{task}.spm.spatial.realign.write.data(n) = cfg_dep(['Realign: Estimate: Realigned Images (Sess ' num2str(n) ')'], substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{n}, '.','cfiles')); end; matlabbatch{task}.spm.spatial.realign.write.roptions.which = [2 1]; matlabbatch{task}.spm.spatial.realign.write.roptions.interp = 4; matlabbatch{task}.spm.spatial.realign.write.roptions.wrap = [0 0 0]; matlabbatch{task}.spm.spatial.realign.write.roptions.mask = 1; matlabbatch{task}.spm.spatial.realign.write.roptions.prefix = 'r'; task = task + 1; d = dir(fullfile(pathname,'anat')); for n=1:numel(d), if contains(d(n).name,'INV2') & ~contains(d(n).name,'gz'), reffilename{1} = fullfile(pathname,'anat',[d(n).name ',1']); n=numel(d); end; end; matlabbatch{task}.spm.spatial.coreg.estimate.ref = reffilename; matlabbatch{task}.spm.spatial.coreg.estimate.source(1) = cfg_dep('Realign: Reslice: Mean Image', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean')); matlabbatch{task}.spm.spatial.coreg.estimate.other(1) = cfg_dep('Realign: Reslice: Resliced Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rfiles')); matlabbatch{task}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi'; matlabbatch{task}.spm.spatial.coreg.estimate.eoptions.sep = [4 2]; matlabbatch{task}.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{task}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7]; task = task + 1; %matlabbatch{task}.spm.spatial.smooth.data(1) = cfg_dep('Realign: Reslice: Resliced Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rfiles')); %matlabbatch{task}.spm.spatial.smooth.data(2) = cfg_dep('Realign: Reslice: Mean Image', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean')); matlabbatch{task}.spm.spatial.smooth.data(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles')); matlabbatch{task}.spm.spatial.smooth.fwhm = [4 4 4]; matlabbatch{task}.spm.spatial.smooth.dtype = 0; matlabbatch{task}.spm.spatial.smooth.im = 0; matlabbatch{task}.spm.spatial.smooth.prefix = 's'; save(fullfile(pathname,'fasl_spm_preprocessing_batch_file'),'matlabbatch'); disp('Done setting up SPM preprocessing batch file.'); disp('Use SPM to load the batch file and run it. When done, use FASL(''SPMLEVEL1'')'); case 'SPMLEVEL1', disp('Setting up SPM level 1 analysis batch file. Please wait...'); spm_jobman('initcfg'); d = dir(fullfile(pathname,'corrected_func')); idx = 1; scanNumbers = []; for n=1:numel(d), if contains(d(n).name,'srcorrected') & contains(d(n).name,'phonation') & ~contains(d(n).name,'gz') & ~contains(d(n).name,'SBRef') & ~contains(d(n).name,'mat') & ~contains(d(n).name,'txt'), filename = d(n).name; [pathname,filename] = fileparts(filename); scanNumbers(idx) = str2num(filename(end-2:end)); fileNumber(idx) = n; idx = idx + 1; end; end; [~,scanOrder]=sort(scanNumbers); disp(numel(scanOrder)); matlabbatch{1}.spm.stats.fmri_spec.dir = {fullfile(pathname,'corrected_func')}; matlabbatch{1}.spm.stats.fmri_spec.timing.units = 'secs'; matlabbatch{1}.spm.stats.fmri_spec.timing.RT = 0.47; matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t = 16; matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t0 = 8; onsets{1} = [0 90]; onsets{2} = [30 60]; d = dir(fullfile(pathname,'corrected_func')); for nSess = 1:6, info = niftiinfo(fullfile(pathname,'corrected_func',d(fileNumber(scanOrder(nSess))).name)); for k = 1:info.ImageSize(4), scans{k,1} = fullfile(pathname,'corrected_func',[d(fileNumber(scanOrder(nSess))).name ',' num2str(k)]); end; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).scans = scans; [filepath,filename] = fileparts(scans{1,1}); multi_reg_filename = ['rp_' filename(3:end) '.txt']; multi_reg_fullpath = {fullfile(pathname,'corrected_func',multi_reg_filename)}; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).name = 'a'; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).onset = onsets{1+mod(nSess-1,2)}; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).duration = 15; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).tmod = 0; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).pmod = struct('name', {}, 'param', {}, 'poly', {}); matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(1).orth = 1; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).name = 'i'; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).onset = onsets{1+mod(nSess,2)};; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).duration = 15; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).tmod = 0; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).pmod = struct('name', {}, 'param', {}, 'poly', {}); matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(2).orth = 1; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).name = 'rest'; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).onset = [15 45 75 105]; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).duration = 15; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).tmod = 0; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).pmod = struct('name', {}, 'param', {}, 'poly', {}); matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).cond(3).orth = 1; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).multi = {''}; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).regress = struct('name', {}, 'val', {}); matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).multi_reg = multi_reg_fullpath; matlabbatch{1}.spm.stats.fmri_spec.sess(nSess).hpf = 128; end; matlabbatch{1}.spm.stats.fmri_spec.fact = struct('name', {}, 'levels', {}); matlabbatch{1}.spm.stats.fmri_spec.bases.hrf.derivs = [0 0]; %matlabbatch{1}.spm.stats.fmri_spec.bases.none = true; matlabbatch{1}.spm.stats.fmri_spec.volt = 1; matlabbatch{1}.spm.stats.fmri_spec.global = 'None'; matlabbatch{1}.spm.stats.fmri_spec.mthresh = 0.8; matlabbatch{1}.spm.stats.fmri_spec.mask = {''}; matlabbatch{1}.spm.stats.fmri_spec.cvi = 'AR(1)'; matlabbatch{2}.spm.stats.fmri_est.spmmat(1) = cfg_dep('fMRI model 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 = 'a>i'; matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [1 -1 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0]; matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'none'; matlabbatch{3}.spm.stats.con.consess{2}.tcon.name = 'i>a'; matlabbatch{3}.spm.stats.con.consess{2}.tcon.weights = [-1 1 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0]; matlabbatch{3}.spm.stats.con.consess{2}.tcon.sessrep = 'none'; matlabbatch{3}.spm.stats.con.delete = 1; save(fullfile(pathname,'fasl_spm_level1_batch_file'),'matlabbatch'); return; FASLx.UserData.InputDirectory.Enable = 'on'; FASLx.UserData.OutputDirectory.Enable = 'on'; FASLx.UserData.deface.Enable = 'off'; FASLx.UserData.topup.Enable = 'on'; FASLx.UserData.normalise.Enable = 'on'; disp('Done setting up SPM preprocessing batch file.'); disp('Use SPM to load the level 1 analysis file and run it. When done, use fsleyes to visualize data.'); otherwise, disp('Did not recognize the command.'); end;