%% Extract the number of voxels associated within ROIs which exceed user specified F threshold.

Fthreshold = input('Specify Threshold: ');


%Read in ROIs
ROIs = { 'BA4', 'BA6', 'BA7')
   
   for r = 1:size(ROIs,2)
      
     fname = ROI{r};
	  finfo = spm_vol(fname);
	  [Y, xyz] = spm_read_vols(finfo,[]);
	  nonzero = find(Y); 
	  Y = Y(nonzero);
	  xyz = xyz(:,nonzero);
   
     eval([ROI{R} '.Y = Y;']);
     eval([ROI{R} '.xyz = xyz;']);
   
   end



% load in Ms , Ps ...

Ms = {'a01', 'a03', 'a05', 'a06', 'a07', 'a09', 'a10','a13', 'a14', 'a15', 'a17', 'a18', 'a20'};

Ps = {'a01', 'a03', 'a05', 'a06', 'a07', 'a09', 'a10','a13', 'a14', 'a15', 'a17', 'a18', 'a20'};


% for each subject: read in HR vector (3000 x #voxels) -> store in M{subject_no)  & P{subject_no) ...


Ms = {'a01', 'a02')
for s = 1:length(Ms)
   

	cd(Ms{2}); cd mr_results; pwd
  
   Fmap = spm_vol('spmF_0001');
   [Yc, xyzc] = spm_read_vols(Fmap,[]);
   
   Fmap_Yc(s).Fmap = Yc;
   Fmap_Yc(s).Fxyz = xyzc;
     
	cd ('../..')

	   
end

Frow = [];
for s = 1:length(Ms)
   
   Yc = Fmap_Yc(s).Fmap  ;
   xyzc = Fmap_Yc(s).Fxyz  ;

   
	ffrow = [];   
	for r = 1:size(ROI,2)
   
   eval(['Y = ' ROI{R} '.Y;']);
   eval(['xyz = ' ROI{R} '.xyz;']);

  
	Fval=[];
		for i = 1:size(xyz,2),
      	ind = find( (xyz(1,i) == xyzc(1,:)) & (xyz(2,i) == xyzc(2,:)) & (xyz(3,i) == xyzc(3,:)) );
      	if ~isempty(ind)
      	ind = ind(1);
      
      	Fval=[Fval; [ xyz(:,i)' ,  Yc(ind ) ] ];
      	end
   
		end

	x = Fval(:,4);
	x = x(find(~isnan(x)));
	Fmean = mean(x);  

	y = find(x > Fthresh)
	num = size(y)

	ffrow = [ffrow , num, mean(x)];
	end %ROI



Frow = [Frow ; ffrow];
end % Sj




   fname = 'Dracula';
   fid = fopen(fname,'w+');
   
   fprintf('Sj 	')
   for j = 1:size(Frow,2)
      fprintf('%4s	', ROI{j})
      fprintf('mean	')

   end
   fprintf('\n');

      
   for i = i:size(Frow, 1)
      
      fprintf('%4s	', Ms{i})
      for j = 1:size(Frow,2)
         fprintf('%4d	',Frow(i,j));
      end
      fprintf('\n');
      
   end
   
   
   fclose(fid)
   
   
   
   
