% This program first generates the excel sheet SCO.xlsx, which summarizes the whole of the SCO data. It shows the error for each subject for 
% consonants in initial position, consonants in final position, vowels in initial position and vowels in final position. The lines 186-203, generate
% scatter plots for errors of vowels and consonants in initial and final position. To generate only SCO.xlsx, comment only lines 186-203, and to generate the 
% scatter plots, comment only lines 178-185.


allsub1=zeros(17,16);
allsub2=zeros(17,16);
allsub3=zeros(17,25);
allsub4=zeros(17,25);
allsub5=zeros(17,40);
allsub6=zeros(17,40);

child={'Alina';'Angela'; 'Anton'; 'Bob';'Carly';'Edward';'Evan';'Joanna';'Latisha';'Laura';'Matt';'Miguel';'Norene';'Shauna';'Teddy';'Tina';'Tony'};

for g=1:length(child)
   load(['C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\Data_'  child{g,1}  '.mat']);
   temfr=p(1,:,:)./N(1,:,:);
   temfr=reshape(temfr,[39 39])';
      
   temba=p(2,:,:)./N(2,:,:);
   temba=reshape(temba,[39 39])';
   
   temppfr=reshape(p(1,:,:),[39 39])';
   tempNfr=reshape(N(1,:,:),[39 39])';
   temppba=reshape(p(2,:,:),[39 39])';
   tempNba=reshape(N(2,:,:),[39 39])';
   
   temppf(g,:)=sum(temppfr,1);
   temppb(g,:)=sum(temppba,1);
   tempNf(g,:)=sum(tempNfr,1);
   tempNb(g,:)=sum(tempNba,1);
   
   matrix;
   
Vowfp=[tempp1(:,1) tempp1(:,2) tempp1(:,5) tempp1(:,7) tempp1(:,9) tempp1(:,10) tempp1(:,13) tempp1(:,14) tempp1(:,15) tempp1(:,17) tempp1(:,19) tempp1(:,21) tempp1(:,25) tempp1(:,30) tempp1(:,35)];
Vowfn=[tempN1(:,1) tempN1(:,2) tempN1(:,5) tempN1(:,7) tempN1(:,9) tempN1(:,10) tempN1(:,13) tempN1(:,14) tempN1(:,15) tempN1(:,17) tempN1(:,19) tempN1(:,21) tempN1(:,25) tempN1(:,30) tempN1(:,35)];
Vowbp=[tempp2(:,1) tempp2(:,2) tempp2(:,5) tempp2(:,7) tempp2(:,9) tempp2(:,10) tempp2(:,13) tempp2(:,14) tempp2(:,15) tempp2(:,17) tempp2(:,19) tempp2(:,21) tempp2(:,25) tempp2(:,30) tempp2(:,35)];
Vowbn=[tempN2(:,1) tempN2(:,2) tempN2(:,5) tempN2(:,7) tempN2(:,9) tempN2(:,10) tempN2(:,13) tempN2(:,14) tempN2(:,15) tempN2(:,17) tempN2(:,19) tempN2(:,21) tempN2(:,25) tempN2(:,30) tempN2(:,35)];
Confp=[tempp3(:,3) tempp3(:,4) tempp3(:,6) tempp3(:,8) tempp3(:,11) tempp3(:,12) tempp3(:,16) tempp3(:,18) tempp3(:,20) tempp3(:,22) tempp3(:,23) tempp3(:,24) tempp3(:,26) tempp3(:,27) tempp3(:,28) tempp3(:,29) tempp3(:,31) tempp3(:,32) tempp3(:,33) tempp3(:,34) tempp3(:,36) tempp3(:,37) tempp3(:,38) tempp3(:,39)];
Confn=[tempN3(:,3) tempN3(:,4) tempN3(:,6) tempN3(:,8) tempN3(:,11) tempN3(:,12) tempN3(:,16) tempN3(:,18) tempN3(:,20) tempN3(:,22) tempN3(:,23) tempN3(:,24) tempN3(:,26) tempN3(:,27) tempN3(:,28) tempN3(:,29) tempN3(:,31) tempN3(:,32) tempN3(:,33) tempN3(:,34) tempN3(:,36) tempN3(:,37) tempN3(:,38) tempN3(:,39)];
Conbp=[tempp4(:,3) tempp4(:,4) tempp4(:,6) tempp4(:,8) tempp4(:,11) tempp4(:,12) tempp4(:,16) tempp4(:,18) tempp4(:,20) tempp4(:,22) tempp4(:,23) tempp4(:,24) tempp4(:,26) tempp4(:,27) tempp4(:,28) tempp4(:,29) tempp4(:,31) tempp4(:,32) tempp4(:,33) tempp4(:,34) tempp4(:,36) tempp4(:,37) tempp4(:,38) tempp4(:,39)];
Conbn=[tempN4(:,3) tempN4(:,4) tempN4(:,6) tempN4(:,8) tempN4(:,11) tempN4(:,12) tempN4(:,16) tempN4(:,18) tempN4(:,20) tempN4(:,22) tempN4(:,23) tempN4(:,24) tempN4(:,26) tempN4(:,27) tempN4(:,28) tempN4(:,29) tempN4(:,31) tempN4(:,32) tempN4(:,33) tempN4(:,34) tempN4(:,36) tempN4(:,37) tempN4(:,38) tempN4(:,39)];

allp1(g,1:15)=sum(Vowfp,1);
alln1(g,1:15)=sum(Vowfn,1);
allp2(g,1:15)=sum(Vowbp,1);
alln2(g,1:15)=sum(Vowbn,1);
allp3(g,1:24)=sum(Confp,1);
alln3(g,1:24)=sum(Confn,1);
allp4(g,1:24)=sum(Conbp,1);
alln4(g,1:24)=sum(Conbn,1);

allsub1(g,1:15)=sum(Vowfp,1)./sum(Vowfn,1);
allsub2(g,1:15)=sum(Vowbp,1)./sum(Vowbn,1);
allsub3(g,1:24)=sum(Confp,1)./sum(Confn,1);
allsub4(g,1:24)=sum(Conbp,1)./sum(Conbn,1);
allsub5(g,1:39)=temppf(g,:)./tempNf(g,:);
allsub6(g,1:39)=temppb(g,:)./tempNb(g,:);

end
allsub1=1-allsub1;
allsub2=1-allsub2;
allsub3=1-allsub3;
allsub4=1-allsub4;
allsub5=1-allsub5;
allsub6=1-allsub6;

allp1(:,16)=sum(allp1(:,1:15),2);
alln1(:,16)=sum(alln1(:,1:15),2);
allp2(:,16)=sum(allp2(:,1:15),2);
alln2(:,16)=sum(alln2(:,1:15),2);
allp3(:,25)=sum(allp3(:,1:24),2);
alln3(:,25)=sum(alln3(:,1:24),2);
allp4(:,25)=sum(allp4(:,1:24),2);
alln4(:,25)=sum(alln4(:,1:24),2);

[Y11,I11]=sort((allp1(3,1:15)+allp1(4,1:15)+allp1(5,1:15)+allp1(7,1:15)+allp1(8,1:15)+allp1(12,1:15))./(alln1(3,1:15)+alln1(4,1:15)+alln1(5,1:15)+alln1(7,1:15)+alln1(8,1:15)+alln1(12,1:15)),'descend');
[Y12,I12]=sort(allp1(1:17,16)./alln1(1:17,16),'descend');
[Y21,I21]=sort((allp2(3,1:15)+allp2(4,1:15)+allp2(5,1:15)+allp2(7,1:15)+allp2(8,1:15)+allp2(12,1:15))./(alln2(3,1:15)+alln2(4,1:15)+alln2(5,1:15)+alln2(7,1:15)+alln2(8,1:15)+alln2(12,1:15)),'descend');
[Y22,I22]=sort(allp2(1:17,16)./alln2(1:17,16),'descend');
[Y31,I31]=sort((allp3(3,1:24)+allp3(4,1:24)+allp3(5,1:24)+allp3(7,1:24)+allp3(8,1:24)+allp3(12,1:24))./(alln3(3,1:24)+alln3(4,1:24)+alln3(5,1:24)+alln3(7,1:24)+alln3(8,1:24)+alln3(12,1:24)),'descend');
[Y32,I32]=sort(allp3(1:17,25)./alln3(1:17,25),'descend');
[Y41,I41]=sort((allp4(3,1:24)+allp4(4,1:24)+allp4(5,1:24)+allp4(7,1:24)+allp4(8,1:24)+allp4(12,1:24))./(alln4(3,1:24)+alln4(4,1:24)+alln4(5,1:24)+alln4(7,1:24)+alln4(8,1:24)+alln4(12,1:24)),'descend');
[Y42,I42]=sort(allp4(1:17,25)./alln4(1:17,25),'descend');

vow={'@' 'A' 'E' 'I' 'O' 'R' 'U' 'W' 'Y' 'a' 'c' 'e' 'i' 'o' 'u'};
cons={'C' 'D' 'G' 'J' 'S' 'T' 'Z' 'b' 'd' 'f' 'g' 'h' 'k' 'l' 'm' 'n' 'p' 'r' 's' 't' 'v' 'w' 'y' 'z'};
exel11(1,2:16)=vow(1,I11);
exel21(1,2:16)=vow(1,I21);
exel31(1,2:25)=cons(1,I31);
exel41(1,2:25)=cons(1,I41);

exel12(1,2:16)=vow(1,I11);
exel22(1,2:16)=vow(1,I21);
exel32(1,2:25)=cons(1,I31);
exel42(1,2:25)=cons(1,I41);

   

for i=1:17
    exel11(i+1,1)=child(I12(i,1),1);
    exel21(i+1,1)=child(I22(i,1),1);
    exel31(i+1,1)=child(I32(i,1),1);
    exel41(i+1,1)=child(I42(i,1),1);
    exel12(i+1,1)=child(I12(i,1),1);
    exel22(i+1,1)=child(I22(i,1),1);
    exel32(i+1,1)=child(I32(i,1),1);
    exel42(i+1,1)=child(I42(i,1),1);
    
    for j=2:16
        if ~isnan(allsub1(I12(i,1),I11(1,j-1)))
            if (1-allsub1(I12(i,1),I11(1,j-1)))<Y11(1,j-1)
            exel12(i+1,j)={round(allsub1(I12(i,1),I11(1,j-1))*100)};
            
            else
            exel12(i+1,j)={''}; 
%             exel11(i+1,j)={''};
            end
        else 
            exel12(i+1,j)={'---'}; 
%             exel11(i+1,j)={'---'};
        end
        exel11(i+1,j)={[num2str(alln1(I12(i,1),I11(1,j-1)) - allp1(I12(i,1),I11(1,j-1))) ' of ' num2str(alln1(I12(i,1),I11(1,j-1)))]};
        
        if ~isnan(allsub2(I22(i,1),I21(1,j-1)))
            if (1-allsub2(I22(i,1),I21(1,j-1)))<Y21(1,j-1)
            exel22(i+1,j)={round(allsub2(I22(i,1),I21(1,j-1))*100)};
            
            else
            exel22(i+1,j)={''}; 
%             exel21(i+1,j)={''};
            end
        else 
            exel22(i+1,j)={'---'}; 
%             exel21(i+1,j)={'---'};
        end
        exel21(i+1,j)={[num2str(alln2(I22(i,1),I21(1,j-1)) - allp2(I22(i,1),I21(1,j-1))) ' of ' num2str(alln2(I22(i,1),I21(1,j-1)))]};
        
            
    end
    
    for j=2:25
        
    if ~isnan(allsub3(I32(i,1),I31(1,j-1)))
            if (1-allsub3(I32(i,1),I31(1,j-1)))<Y31(1,j-1)
            exel32(i+1,j)={round(allsub3(I32(i,1),I31(1,j-1))*100)};
           
            else
            exel32(i+1,j)={''}; 
%             exel31(i+1,j)={''};
            end
        else 
            exel32(i+1,j)={'---'}; 
%             exel31(i+1,j)={'---'};
    end
         exel31(i+1,j)={[num2str(alln3(I32(i,1),I31(1,j-1)) - allp3(I32(i,1),I31(1,j-1))) ' of ' num2str(alln3(I32(i,1),I31(1,j-1)))]};
         
        if ~isnan(allsub4(I42(i,1),I41(1,j-1)))
            if (1-allsub4(I42(i,1),I41(1,j-1)))<Y41(1,j-1)
            exel42(i+1,j)={round(allsub4(I42(i,1),I41(1,j-1))*100)};
           
            else
            exel42(i+1,j)={''}; 
%             exel41(i+1,j)={''};
            end
        else 
            exel42(i+1,j)={'---'}; 
%             exel41(i+1,j)={'---'};
        end
         exel41(i+1,j)={[num2str(alln4(I42(i,1),I41(1,j-1)) - allp4(I42(i,1),I41(1,j-1))) ' of ' num2str(alln4(I42(i,1),I41(1,j-1)))]};  
    end  
    
    
end    


xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel32,1,'A1:Y18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel31,2,'A1:Y18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel42,3,'A1:Y18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel41,4,'A1:Y18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel12,5,'A1:P18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel11,6,'A1:P18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel22,7,'A1:P18');
xlswrite('C:\Users\Anjali\Documents\MATLAB\NewFolder1\Reading Group\RGT-SCO_results\SCO.xlsx',exel21,8,'A1:P18');


% This part of the program generates scatter plots of vowels and consoants
% in initial and final position

% C=[0 0 1;0 0 1;1 0 0;1 0 0;1 0 0;0 0 1;1 0 0;1 0 0;0 0 1;0 0 1;0 0 1;1 0 0;0 0 1;0 0 1;0 0 1;0 0 1;0 0 1]
%     figure(1)
%     scatter((1-(allp1(:,16)./alln1(:,16))),(1-(allp2(:,16)./alln2(:,16))),25,C,'filled')
%     xlabel('Vowel error in initial position')
%     ylabel('Vowel error in final position')
%     title('Scatter plots for vowel errors in initial versus final position')
%     for l=1:17
%         text((1-(allp1(l,16)./alln1(l,16))),(1-(allp2(l,16)./alln2(l,16))),child{l,1});
%     end
%     
%     figure(2)
%     scatter((1-(allp3(:,25)./alln3(:,25))),(1-(allp4(:,25)./alln4(:,25))),25,C,'filled')
%     xlabel('Consonant error in initial position')
%     ylabel('Consonant error in final position')
%     title('Scatter plots for consonant errors in initial versus final position')
%     for l=1:17
%         text((1-(allp3(l,25)./alln3(l,25))),(1-(allp4(l,25)./alln4(l,25))),child{l,1});
%     end
    
    
