function V = Vols_Stats(Images,CodeFile,OutputFile);
%
% Syntax: V = Vols_Stats(Images,CodeFile,OutputFile);
%
% This function computes some statistic measures(mean and standar deviation) 
% and save the results into a file for all the structures previously obtained 
% by the function struct_vol. 
%
% Input Parameters:
%        Images : Atlased Files 
%      CodeFile : File with the structure's names and codes(*.cod) 
%    OutputFile : Output File Name where Statistic will be saved. 
%
% Output Parameters:
%       V : Matrix of size: Number of subjects  x  Number of structures . 
%           The element V(i,j) express the volume of the structure "j" for 
%           the subject "i".  
%
% See also:  struct_vol 
%__________________________________________________
% Authors: Lester Melie García & Yasser Alemán Gómez
% Neuroimaging Department
% Cuban Neuroscience Center
% Last update: November 15th 2005
% Version $1.0

%=====================Checking Input Parameters===========================%
if ((nargin<1)|isempty(Images))
    [Images,sts] = spm_select([1 Inf],'image','Selecting Atlased Images','',cd);
    [NFiles,m] = size(Images);
end;
if ~exist('CodeFile','var')
   [CodeFileName,CodeFilePath] = uigetfile('*.cod','Reading Strutures Code File ...');
   CodeFile = [CodeFilePath CodeFileName];
   disp(['Structures Code File : ' CodeFileName]);
end;
if isempty(CodeFile)
   [CodeFileName,CodeFilePath] = uigetfile('*.cod','Reading Strutures Code File ...');
   CodeFile = [CodeFilePath CodeFileName];
   disp(['Structures Code File : ' CodeFileName]);
end;
if ~exist('OutputFile','var')
  [OutputFileName,OutputFilePath] = uiputfile('*.txt','Saving Volume of Structures ...');
  OutputFile = [OutputFilePath OutputFileName];
end;
if isempty(OutputFile)
  [OutputFileName,OutputFilePath] = uiputfile('*.txt','Saving Volume of Structures ...');
  OutputFile = [OutputFilePath OutputFileName];
end;
% ========================================================================%
%
%=========================Main program====================================%
[pth,nm,ext] = fileparts(Images(1,:));
FileName = [pth filesep nm ext(1:4)];
Va = spm_vol(FileName);
voxsize = sqrt(sum(Va.mat(1:3,1:3).^2));
vols = Struct_Vol(FileName,CodeFile,voxsize);
disp('Case -->  1'); 
Nstruct = length(vols);
[NFiles,m] = size(Images);
V = zeros(NFiles,Nstruct);
for k=1:Nstruct
    V(1,k) = vols(k).vol; 
end;
for i=2:NFiles
   disp(['Case -->  ' num2str(i)]);
   [pth,nm,ext] = fileparts(Images(i,:));
   FileName = [pth filesep nm ext(1:4)];
   Va = spm_vol(FileName);
   voxsize = sqrt(sum(Va.mat(1:3,1:3).^2));
   vols = struct_vol(FileName,CodeFile,voxsize);
   for k=1:Nstruct
       V(i,k) = vols(k).vol; 
   end;
end;
for i=1:Nstruct
    a(i) = length(vols(i).name);
end;
L = max(a); % Maximum Length of Structure Names
fid = fopen(OutputFile,'w');
S0 = repmat(' ',1,round(L/2)-round(length('Structure')/2));
S1 = repmat(' ',1,round(L/2));
if NFiles==1
   V = V(:);
   fprintf(fid,'%s%s%s%s    \r',S0,'Structure',S1,'Volume (cm^3)');
   fprintf(fid,'\r');
   for i=1:Nstruct
      sL = length(vols(i).name);
      sp = L-sL;
      S = repmat('_',1,sp);
      fprintf(fid,'%s  %s  %8.4f\r',vols(i).name,S,V(i));
   end;
else
   Vmean = mean(V); Vmean = Vmean(:);
   Vstd = std(V); Vstd = Vstd(:);
   Stat = [Vmean Vstd];
   fprintf(fid,'%s%s%s%s    %s \r',S0,'Structure',S1,'Mean (cm^3)','STD (cm^3) ');
   fprintf(fid,'\r');
   for i=1:Nstruct
      sL = length(vols(i).name);
      sp = L-sL;
      S = repmat('_',1,sp);
      fprintf(fid,'%s  %s  %8.4f     %8.4f\r',vols(i).name,S,Stat(i,1),Stat(i,2));
   end;
end;
fclose(fid);
save([OutputFile(1:end-4) '.mat'],'V')
%========================End of main program==============================%


