function rd_motion;

% Computes correlations between task and motion.
% FORMAT rd_motion
%
% ---------------------------------------------------------------
% @(#) rd_motion.m 2.0      Kalina Christoff         2001-04-12 

fprintf('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n');
fprintf('@(#) rd_motion.m 2.0                    Kalina Christoff    2001-04-12');
fprintf('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n\n');

n_subj = input('Enter number of subjects : ');

for s = 1:n_subj
  
  SPMcfg_path = spm_get(1,'SPMcfg.mat',...
				 sprintf('Select SPMcfg.mat file for Subject %d',s));
  load(SPMcfg_path);    
  
  % get # sessions
  n_sess         = length(Sess);
  
  % motion parameters
  M_path  =  spm_get(n_sess,'.txt',...
		    'Select all motion param files for this subject.');
  M=[];
  for sess=1:n_sess
     M=[M; spm_load(deblank(M_path(sess,:)))];
  end

  % esp for int_ext, where the first 20 files are excluded
  M
  
  % build motion param matrices
  tmp=xX.X(:,xX.iB);
  for i = 1:6
	 tmp(find(xX.X(:,xX.iB)))=M(:,i);
    mmtr{i}=[xX.X,tmp];
  end
  
  % compute condition numbers
  for i = 1:6
	 cmmtr(i) = cond(mmtr{i});
  end
  
  % pack things
  S(s).SPMcfg_path = SPMcfg_path;
  S(s).M = M;
  S(s).mmtr = mmtr;
  S(s).cmmtr = cmmtr;

end

   % print results

   fprintf('\n----------------------------------------------------------------------\n');
	fprintf(['Condition numbers resulting from adding each motion parameter', ...
				' to the\ndesign matrix.  High condition number indicates', ...
				' high collinearity.\n-----------------------',...
				'-----------------------------------------------\n\n']);
   fprintf('%23s%6s%6s%6s%7s%7s%7s%10s\n\n','Subject',...
                'x_mm','y_mm','z_mm','pitch','roll','yaw','sum(e+3)');
	for s=1:n_subj
	  spath = S(s).SPMcfg_path; 
	  if length(spath) > 22 
		 spath = ['..',spath(end-31:end-11)]; 
	  end
	 
   fprintf('%23s%6.0f%6.0f%6.0f%7.0f%7.0f%7.0f%8.2f\n',...
                spath,S(s).cmmtr,(sum(S(s).cmmtr)/1000));
	end
	
	fprintf(['\n--------------------------------------------------------',...
			   '--------------\nThe numbers listed in the "sum(e+3)" column',...
			   ' can be entered as a nuisance\nvariable in a group-level ANCOVA',...
				' to remove motion-related variance.\n\n']);







