    
% FORMAT     subject = roi_extract_many
%            subject = roi_extract_many(Subs, ROIs, ImgF)
% _______________________________________________________________________________
% Given a list of subject-directories, ROIs and image-files, this function computes 
% the means of these ROIs in these image-files, for the given subjects.
% 
% If the inputs are not provided, SPM prompts the user to select the subject
% directories, ROI(.tal) files and the image files.
%
% By default, the filter(for image files) is 'beta*.img', since the most common use 
% of this function is probably to get the ROI-means for beta images. The user is free 
% to change this file filter to other options such as *.img or con*.img in the 
% file-selection window.
% 
% The output is a matlab structure and the values can be accessed as follows:
% subject(s).ROI(r).value(i) = Mean of the ith image of the rth ROI of the sth subject, or as:
% subject(s).ROI(r).value    = Ordered list of all computed means for rth ROI of sth subject
%
% Other information, useful for later recollection, and for programmers:
% subject(s).subject         = Subject-directory of the sth subject
% subject(s).ROI(r).ROIname  = name of the rth ROI for the sth(actually, any) subject
% subject(s).imfile(i)       = ith image file for the sth subject
% subject(s).imfile          = ordered list of image files for the sth subject ...
%
% Important: The subject-directories chosen _must_ be the immediate parent directory
% for the image files. This actually makes it convenient for the user, since it is 
% sufficient to select the different subject directories and the image-files for 
% _one_ subject. The program then repeats the computations for thse image-files for 
% other subjects. So we assume that your directory structure is consistent, and the 
% image-file names are the same for different subjects. If not, then you can run 
% this program once for every subject.
% 
% Notes: 
% - the ROI-mean extraction for individual ROIs is done by Sue Whitfield's roi_extract.m
%   so that program must be included in your matlab path.
%_______________________________________________________________________________
%   Pavithra Srinivasan, 24th January 2003.
%   Text output added Jeff Cooper, 20th Feb 2003.
%   For questions or comments please contact pavisrin@stanford.edu
%   Online documentation will be put up shortly on gablab.stanford.edu/brainiac




function subject = roi_extract_many(Subs, ROIs, ImgF);


if nargin == 0
  Subs = spm_get(-inf,'.', 'Select the Individual Subject directories');
end

%if nargin < 4
%  Filter = 'beta*.img'
%end
  
if nargin < 2
  ImgF = spm_get(Inf, 'beta*.img','Select image files to extract from');
end

if nargin < 3
  ROIs = spm_get(Inf,'*.tal','Select .tal files defining ROIs');
end


ImgF  = spm_str_manip(ImgF, 't'); % now the image-file names are just names, ie sans the path
nSubs = size(Subs, 1);  % number of subjects
nROIs = size(ROIs, 1);  % number of ROI files
nImgs = size(ImgF, 1);  % number of image files

% 'ROI' and 'subject' are structures that will store the information, so they must be cleared 
% if they already exist(ie the user is runing the program again)
clear ROI;
clear subject;

for s = 1 : nSubs,     % for the sth subject
    % forming the image file names(with the right paths) for the current subject:
    clear imfilename;
    for i = 1 : nImgs, % for the ith image file
        imfilename(i, :) = fullfile(deblank(Subs(s, :)), deblank(ImgF(i, :)));        
    end
    
    for r = 1 : nROIs, % for the rth ROI,
    
        ROIname = spm_str_manip(ROIs(r, :), 'sd');
        values  = roi_extract(imfilename, ROIname, ''); %the '' is for not writing a text file
        % 'values' is now an array of the roi-means for the given images
    
        ROI(r)  = struct('ROIname', [ROIname], 'value', [values]);
    
    end
    
    subject(s)  = struct('subject', [deblank(Subs(s, :))],  'imfile', [imfilename], 'ROI', ROI);
end

fname = fullfile(pwd, 'roi_extract_many_output.txt');
fid = fopen(fname,'w');
if fid == -1, error(['Unable to create new file - please check permissions', ...
				  ' in current directory.']); 
end
    
for i = 1:nSubs
    fprintf(fid, 'Subject:\t%s\n', deblank(Subs(i, :)));
    fprintf(fid, '-------------------------\n');
    for j = 1:nROIs
        fprintf(fid, '\tROI:\t%s\n', spm_str_manip(ROIs(j,:), 'tsd'));
        for k = 1:nImgs
            fprintf(fid, '\t\tImg:\t%s\tVal:\t%4.4f\n', deblank(ImgF(k, :)), ...
                    subject(i).ROI(j).value(k));
        end
    end
    fprintf(fid, '-------------------------\n\n\n');
end
            
            

fclose(fid);

[dir, name] = fileparts(fname);
fprintf('\n\nThe output file %s.txt was created in %s\n\n',name, dir);



