% The program generates the ps file barplots5.ps. I usually convert it to pdf online. For each subject, four bar plots are generated, one each for
% consonants in initial position, consonants in final position, vowels in initial position and vowels in final position. The rounded percentage 
% error is displayed above the bar and the number of presentations are displyed above that.

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']);
matrix;
vowfr=1-(sum(tempp1,1)./sum(tempN1,1));
vowba=1-(sum(tempp2,1)./sum(tempN2,1));
confr=1-(sum(tempp3,1)./sum(tempN3,1));
conba=1-(sum(tempp4,1)./sum(tempN4,1));

vowfr1=[vowfr(:,1) vowfr(:,2) vowfr(:,5) vowfr(:,7) vowfr(:,9) vowfr(:,10) vowfr(:,13) vowfr(:,14) vowfr(:,15) vowfr(:,17) vowfr(:,19) vowfr(:,21) vowfr(:,25) vowfr(:,30) vowfr(:,35)];
vowba1=[vowba(:,1) vowba(:,2) vowba(:,5) vowba(:,7) vowba(:,9) vowba(:,10) vowba(:,13) vowba(:,14) vowba(:,15) vowba(:,17) vowba(:,19) vowba(:,21) vowba(:,25) vowba(:,30) vowba(:,35)];
confr1=[confr(:,3) confr(:,4) confr(:,6) confr(:,8) confr(:,11) confr(:,12) confr(:,16) confr(:,18) confr(:,20) confr(:,22) confr(:,23) confr(:,24) confr(:,26) confr(:,27) confr(:,28) confr(:,29) confr(:,31) confr(:,32) confr(:,33) confr(:,34) confr(:,36) confr(:,37) confr(:,38) confr(:,39)];
conba1=[conba(:,3) conba(:,4) conba(:,6) conba(:,8) conba(:,11) conba(:,12) conba(:,16) conba(:,18) conba(:,20) conba(:,22) conba(:,23) conba(:,24) conba(:,26) conba(:,27) conba(:,28) conba(:,29) conba(:,31) conba(:,32) conba(:,33) conba(:,34) conba(:,36) conba(:,37) conba(:,38) conba(:,39)];

[Y1,I1]=sort(vowfr1,2,'descend');
[Y2,I2]=sort(vowba1,2,'descend');
[Y3,I3]=sort(confr1,2,'descend');
[Y4,I4]=sort(conba1,2,'descend');

vow1={'@' 'A' 'E' 'I' 'O' 'R' 'U' 'W' 'Y' 'a' 'c' 'e' 'i' 'o' 'u'};
cons1={'C' 'D' 'G' 'J' 'S' 'T' 'Z' 'b' 'd' 'f' 'g' 'h' 'k' 'L' 'm' 'n' 'p' 'r' 's' 't' 'v' 'w' 'y' 'z'};

figure(1)
subplot(2,1,1)

bar(Y1)
colormap summer;
ylim([0 1.1])
ylabel('Error')
title([child{g,1} ' Vowel in Initial Position Errors'])

for i=1:15
    Xlab(i,1)=vow1{1,I1(1,i)};
    text(i-0.3,Y1(1,i)+0.04,num2str(round(Y1(1,i)*100)));
    text(i-0.3,Y1(1,i)+0.14,num2str(sum(tempN1(:,ind1(1,I1(1,i)+1)),1)))
end

set(gca,'XTick',1:15)
set(gca,'XTickLabel',Xlab)

subplot(2,1,2)
bar(Y2)
colormap summer;
ylim([0 1.1])
ylabel('Error')
title([child{g,1} ' Vowel in Final Position Errors'])
for i=1:15
    Xlab(i,1)=vow1{1,I2(1,i)};
    text(i-0.3,Y2(1,i)+0.04,num2str(round(Y2(1,i)*100)));
    text(i-0.3,Y2(1,i)+0.14,num2str(sum(tempN2(:,ind1(1,I2(1,i)+1)),1)))
end

set(gca,'XTick',1:15)
set(gca,'XTickLabel',Xlab)

    if g==1
    psname= 'barplots5';
    print ( '-dpsc2', psname, '-f1' )
    else
    print ( '-dpsc2', psname, '-append', '-f1' ) 
    end
    close
    
figure(2)

subplot(2,1,1)

bar(Y3)
colormap summer;
ylim([0 1.1])
ylabel('Error')
title([child{g,1} ' Consonant in Initial Position Errors'])

for i=1:24
    Xlab(i,1)=cons1{1,I3(1,i)};
    text(i-0.3,Y3(1,i)+0.04,num2str(round(Y3(1,i)*100)));
    text(i-0.3,Y3(1,i)+0.14,num2str(sum(tempN3(:,ind2(1,I3(1,i)+1)),1)))
end

set(gca,'XTick',1:24)
set(gca,'XTickLabel',Xlab)

subplot(2,1,2)
bar(Y4)
colormap summer;
ylim([0 1.1])
ylabel('Error')
title([child{g,1} ' Consonant in Final Position Errors'])
for i=1:24
    Xlab(i,1)=cons1{1,I4(1,i)};
    text(i-0.3,Y4(1,i)+0.04,num2str(round(Y4(1,i)*100)));
    text(i-0.3,Y4(1,i)+0.14,num2str(sum(tempN4(:,ind2(1,I4(1,i)+1)),1)))
end

set(gca,'XTick',1:24)
set(gca,'XTickLabel',Xlab)
    
print ( '-dpsc2', psname, '-append', '-f2' )  
close

end
   
