Home > marsbar > examples > batch > estimate_er_model.m

estimate_er_model

PURPOSE ^

SPM estimate of ER model, and add contrast to ER model

SYNOPSIS ^

function SPM = estimate_er_model(model_file, ev_con)

DESCRIPTION ^

 SPM estimate of ER model, and add contrast to ER model
 
 model_file      - path to directory containing model

 Single or multisesson analyses.

 $Id: estimate_er_model.m,v 1.1.1.1 2004/08/14 00:07:52 matthewbrett Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function SPM = estimate_er_model(model_file, ev_con)
0002 % SPM estimate of ER model, and add contrast to ER model
0003 %
0004 % model_file      - path to directory containing model
0005 %
0006 % Single or multisesson analyses.
0007 %
0008 % $Id: estimate_er_model.m,v 1.1.1.1 2004/08/14 00:07:52 matthewbrett Exp $
0009 
0010 if nargin < 1
0011   error('Need model filename');
0012 end
0013 if nargin < 2
0014   error('Need event contrast');
0015 end
0016 
0017 % Check for model
0018 if ~exist(model_file, 'file')
0019   error(['Cannot find ' model_file]);
0020 end
0021 SPM = load(model_file);
0022 if isfield(SPM, 'SPM'), SPM=SPM.SPM; end
0023 
0024 % Work out contrast, taking into account no of sessions
0025 nblocks = length(SPM.xX.iB);  % number of sessions in this model
0026 con = [repmat(ev_con, 1, nblocks) zeros(1, nblocks)]';
0027 
0028 % new path, store path, move to model path
0029 swd = fileparts(model_file);
0030 pwd_orig = pwd;
0031 
0032 switch spm('ver')
0033  case 'SPM99'
0034   % Estimate parameters
0035   cd(swd);
0036   Sess=SPM.Sess; xsDes=SPM.xsDes;       % because spm_spm uses inputname
0037   spm_spm(SPM.VY,SPM.xX,SPM.xM,SPM.F_iX0,Sess,xsDes);
0038   
0039   % add contrasts, estimate all contrasts
0040   cd(pwd_orig);
0041   global SPM_BCH_VARS
0042   con_struct = struct('names', {{'stim_hrf'}},...
0043               'types', {{'T'}}, ...
0044               'values', {{con'}}); 
0045 
0046   % Batch directory should be current directory, but maybe not
0047   m_file = 'er_contrast_spm99';
0048   ms = which(m_file);
0049   if isempty(ms)
0050     error(sprintf(['Hmm - can''t find %s on the path - maybe ' ...
0051            'you should run from the batch directory'], ...
0052           m_file))
0053   end
0054   
0055   SPM_BCH_VARS = struct(...
0056       'work_dir', swd, ...
0057       'ana_type', 2, ...          % contrasts
0058       'm_file', ms, ...
0059       'contrasts', con_struct);
0060   spm_bch('do_bch_wrapper');
0061   
0062  otherwise
0063   % load SPM defaults
0064   if ~exist('defaults', 'var')
0065     global defaults;
0066     spm_defaults; 
0067   end
0068 
0069   % Estimate parameters
0070   cd(swd);
0071   spm_unlink(fullfile('.', 'mask.img')); % avoid overwrite dialog
0072   SPM = spm_spm(SPM);
0073   
0074   % add contrast, estimate all contrasts
0075   new_con = spm_FcUtil('Set','stim_hrf',...
0076                'T','c',con,SPM.xX.xKXs);
0077   if isempty(SPM.xCon)
0078     SPM.xCon = new_con;
0079   else
0080     SPM.xCon(end + 1) = new_con;
0081   end
0082   SPM = spm_contrasts(SPM);
0083 
0084   % Back to where we started
0085   cd(pwd_orig);
0086 end
0087 

Generated on Wed 11-May-2022 15:34:44 by m2html © 2003-2019