%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read in data from both methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
load Left1.dat
load Left2.dat
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find number of regions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
s = size(Left1);
nregions = s(1);
nbrains  = s(2);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Build rtable table
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
rtable = cell(36,6);
%
rtable{1,1} = 'Region';
rtable{1,2} = 'Mean (method 1)';
rtable{1,3} = 'Mean (method 2)';
rtable{1,4} = 'Best method';
rtable{1,5} = 'Significance';
rtable{1,6} = 'P-value';
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Analyse all regions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
for i = 1:nregions
        val1 = Left1(i,:);
        val2 = Left2(i,:);
        m1   = mean(val1);
        m2   = mean(val2);
        rtable{i+1,1}=i;
        rtable{i+1,2}=m1;
        rtable{i+1,3}=m2;
        if m1 > m2
                rtable{i+1,4} = 'Method 2';
        else
                rtable{i+1,4} = 'Method 1';
        end
        [h,p]=ttest2(val1,val2);
        rtable{i+1,6} = p;
        if p < 0.01
                rtable{i+1,5} = 'Y';
        else
                rtable{i+1,5} = 'N';
        end
end

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate plot as a bar graph that shows the difference of the mean, with significant
% differences in blue, and non significant differences in red
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
n_significant = 0;
n_nonsignificant = 0;
for i = 1:35
         if rtable{i+1,6} < 0.01
                  n_significant = n_significant + 1;
                  region_sig(n_significant) = i;
                  diff_sig(n_significant) = rtable{i+1,2}-rtable{i+1,3};
         else
                  n_nonsignificant = n_nonsignificant + 1;
                  region_nonsig(n_nonsignificant) = i;
                  diff_nonsig(n_nonsignificant) = rtable{i+1,2}-rtable{i+1,3};
          end
end
%
bar(region_sig,diff_sig,'b');
hold on
bar(region_nonsig,diff_nonsig,'r')
%
val=[1:1:35];
xticks(val);
axis tight;
xlabel('Region #');
ylabel('Mean (method1) - Mean(method2)');
legend('Significant','Not significant');
title('Comparing two methods for brain surface matching');
