function [estimateComp]=nic_rsdd_estimateCompNum(appConfig)
% Estimate the number of components
% Input: 
%	appConfig - structure containing all parameters 
% Output:
%	estimateComp - the number of components estimated
%-----------------------------------------------------------
%   Copyright (C) 2009 Neuroimage Computing Group, State Key Laboratory of
%   Cognitive Neuroscience and Learning, Beijing normal university
%	Written by Gwen Chen
%	Version=1.0;
%	Release=20090726;
%-----------------------------------------------------------
%

disp('..............................................');
disp('START TO ESIMATE THE NUMBER OF COMPONENT');    

maskFile=appConfig.dataReduction.MaskFile;
estimateComp = 0;
 
 for numFiles = 1:length(appConfig.AppSubjectLabels)
       disp(fprintf('.........Estimate component number of subject %d',numFiles));
       [ estCompVec(numFiles), mdlVal, aicVal ] = nic_icatb_estimate_dimension( ...
                    appConfig.AppFileList{numFiles,3}.name, maskFile);
       if length(appConfig.AppSubjectLabels) > 1
            if numFiles == 1
                meanMdl = zeros(size(mdlVal));
             end
            meanMdl = meanMdl + mdlVal;
       end

        mdl_sub(numFiles) = estCompVec(numFiles);

        estimateComp = estCompVec(numFiles) + estimateComp;
        
        nic_waitbar(numFiles/length(appConfig.AppSubjectLabels),sprintf('Estimate the number of component...'), 'Multiple-runs ICA (Preprocessing)','Child');
        
end
            % calculate mean for same points information
if length(appConfig.AppSubjectLabels) > 1
      meanMdl = meanMdl / length(appConfig.AppSubjectLabels);
end
estimateComp = round(estimateComp/length(appConfig.AppSubjectLabels));
                
meanSub = mean(mdl_sub); stdSub = std(mdl_sub); [minSub] = min(mdl_sub); [maxSub] = max(mdl_sub);
string1 = ['1. The estimated independent components is found to be ', num2str(estimateComp), ...
                ' using the MDL criteria.'];
string2 = ['2. The mean of mdl over all subjects is found to be ', num2str(meanSub), '.'];
string3 = ['3. The standard deviation of mdl over all subjects is found to be ', num2str(stdSub), '.'];
string4 = ['4. The minimum of mdl over all subjects is found to be ', num2str(minSub), '.'];
string5 = ['5. The maximum of mdl over all subjects is found to be ', num2str(maxSub), '.'];
string6 = ['6. The median of mdl over all subjects is found to be ', num2str(median(mdl_sub)), '.'];
disp(string1);
disp(string2);
disp(string3);
disp(string4);
disp(string5);
disp(string6);



