% This program is used to create the confusion matrices for each subject. To run this program, write the name of teh subject in the 
% command on line 12, and then again the same name in lines 620-623 to name the excel sheets. This programs needs to be run once for each individual
% subject. It scans through each line, first finding whether the sound is a CV or VC, and thereafter checking for insertions, deletions and 
% inversions.

%Here is how it worked before I messed with it
% load DataNSCM/DataNSCM_Carly.mat
% name=DataNSCM_Carly
%Therefore use: name = load('DataNSCM/DataNSCM_Carly.mat')

%clear all;

%Reprocess three subjects with errors
%sname='Evan';
%sname='Angela';
%sname='Teddy';
sname='Edward';

%sname='Carly' %testcase to prove that program works


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

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

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

  
 for m=1:15
 sname = child{m};

aname=['DataNSCM_',sname];
fname=['DataNSCM/DataNSCM_',sname];
disp(sprintf('Filename= %s',fname))

%The problem is to load data from matfile, and save it in a cell
%array called "name"; The load command returns a structure, which
% must be converted into a cell array
%The second argument is necessary when there is more than one element in the mat file
%  (e.g., ans = 'fo')
x=struct2cell(load(fname,aname));
name=x{1}; %dereference the 1x1 cell array
clear x;

%Init some arrays
    consf=zeros(25,29);consfs{25,1}=[];
    consb=zeros(25,29);consbs{25,1}=[];
    vowf=zeros(15,19);vowfs{15,1}=[];
    vowb=zeros(15,19);vowbs{15,1}=[];
    
    
for i=2:length(name)
          temp1=[];temp2=[];temp3=[];temp4=[];
          ind=strfind(name{i,5},','); 
          if isempty(ind)
              temp1{1,1}=name{i,5};
          else    
          for k=1:length(ind)+1
              if k==1
                  temp1{k,1}= name{i,5}(1:ind(1,1)-1);
              else
                  if k==length(ind)+1
                      temp1{k,1}=name{i,5}(ind(1,length(ind))+1:end);
                  else    
                      temp1{k,1}= name{i,5}(ind(1,k-1)+1:ind(1,k)-1);
                  end    
              end          
          end
          end      
          
          if ~isnan(name{i,6})
          ind=strfind(name{i,6},','); 
          if isempty(ind)
              temp2{1,1}=name{i,6};
          else    
          for k=1:length(ind)+1
              if k==1
                  temp2{k,1}= name{i,6}(1:ind(1,1)-1);
              else
                  if k==length(ind)+1
                      temp2{k,1}=name{i,6}(ind(1,length(ind))+1:end);
                  else    
                      temp2{k,1}= name{i,6}(ind(1,k-1)+1:ind(1,k)-1);
                  end    
              end          
          end
          end
          end
          
          if ~isnan(name{i,7})
          ind=strfind(name{i,7},',');
          if isempty(ind)
              temp3{1,1}=name{i,7};
          else    
          for k=1:length(ind)+1
              if k==1
                  temp3{k,1}= name{i,7}(1:ind(1,1)-1);
              else
                  if k==length(ind)+1
                      temp3{k,1}=name{i,7}(ind(1,length(ind))+1:end);
                  else    
                      temp3{k,1}= name{i,7}(ind(1,k-1)+1:ind(1,k)-1);
                  end    
              end          
          end
          end 
          end
          
          if ~isnan(name{i,8})
          ind=strfind(name{i,8},',');
          if isempty(ind)
              temp4{1,1}=name{i,8};
          else    
          for k=1:length(ind)+1
              if k==1
                  temp4{k,1}= name{i,8}(1:ind(1,1)-1);
              else
                  if k==length(ind)+1
                      temp4{k,1}=name{i,8}(ind(1,length(ind))+1:end);
                  else    
                      temp4{k,1}= name{i,8}(ind(1,k-1)+1:ind(1,k)-1);
                  end    
              end          
          end
          end
          end
          
  if ~isempty(strfind('r',name{i,4}(1))) || ~isempty(strfind('r',name{i,4}(2))) || ~isempty(strfind('R',name{i,4}(1))) || ~isempty(strfind('R',name{i,4}(2))) 
      if ~isempty(temp1)
          for f1=1:length(temp1)
              for f2=1:length(temp1{f1,1})
                 if (~isempty(strfind('r',name{i,4}(1))) || ~isempty(strfind('r',name{i,4}(2)))) && ~isempty(strfind('R',temp1{f1,1}(f2)))
                     temp1{f1,1}(f2)='r';
                 elseif (~isempty(strfind('R',name{i,4}(1))) || ~isempty(strfind('R',name{i,4}(2)))) && ~isempty(strfind('r',temp1{f1,1}(f2))) 
                     temp1{f1,1}(f2)='R';
                 end    
              end
          end
      end
      
      if ~isempty(temp2)
          for f1=1:length(temp2)
              for f2=1:length(temp2{f1,1})
                 if (~isempty(strfind('r',name{i,4}(1))) || ~isempty(strfind('r',name{i,4}(2)))) && ~isempty(strfind('R',temp2{f1,1}(f2)))
                     temp2{f1,1}(f2)='r';
                 elseif (~isempty(strfind('R',name{i,4}(1))) || ~isempty(strfind('R',name{i,4}(2)))) && ~isempty(strfind('r',temp2{f1,1}(f2))) 
                     temp2{f1,1}(f2)='R';
                 end    
              end
          end
      end
      
      if ~isempty(temp3)
          for f1=1:length(temp3)
              for f2=1:length(temp3{f1,1})
                 if (~isempty(strfind('r',name{i,4}(1))) || ~isempty(strfind('r',name{i,4}(2)))) && ~isempty(strfind('R',temp3{f1,1}(f2)))
                     temp3{f1,1}(f2)='r';
                 elseif (~isempty(strfind('R',name{i,4}(1))) || ~isempty(strfind('R',name{i,4}(2)))) && ~isempty(strfind('r',temp3{f1,1}(f2))) 
                     temp3{f1,1}(f2)='R';
                 end    
              end
          end
      end
      
      if ~isempty(temp4)
          for f1=1:length(temp4)
              for f2=1:length(temp4{f1,1})
                 if (~isempty(strfind('r',name{i,4}(1))) || ~isempty(strfind('r',name{i,4}(2)))) && ~isempty(strfind('R',temp4{f1,1}(f2)))
                     temp4{f1,1}(f2)='r';
                 elseif (~isempty(strfind('R',name{i,4}(1))) || ~isempty(strfind('R',name{i,4}(2)))) && ~isempty(strfind('r',temp4{f1,1}(f2))) 
                     temp4{f1,1}(f2)='R';
                 end    
              end
          end
      end
      
      
      
  end    
         
      
        
      
ind2=strfind(cons,name{i,4}(1));

if isempty(ind2)
    
    %Find the indices for the row
    % Vowel front consonant back
    
    indv=strfind(vow,name{i,4}(1));
    indc=strfind(cons,name{i,4}(2));
    
    %Settle the flags
    
    if ~isempty(temp1)
    for l=1:length(temp1)
        if length(temp1{l,1})>2
            vowf(indv,16)=vowf(indv,16)+1;
            consb(indc,26)=consb(indc,26)+1;
            if ~isempty(strfind(temp1{l,1},name{i,4}(1))) && ~isempty(strfind(temp1{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consb(indc,ind7)=consb(indc,ind7)+1;
                    vowf(indv,ind6)=vowf(indv,ind6)+1; 
                    
                else
                    vowfs{indv,1}=[vowfs{indv,1} ' ' num2str(i)];
                    consbs{indc,1}=[consbs{indc,1} ' ' num2str(i)];
            end    
        elseif length(temp1{l,1})<2
            vowf(indv,17)=vowf(indv,17)+1;
            consb(indc,27)=consb(indc,27)+1;
        else    
            ind3=strfind(vow,temp1{l,1}(1));
            if isempty(ind3)
                vowf(indv,18)=vowf(indv,18)+1;
                consb(indc,28)=consb(indc,28)+1;
            end
        end  
        
        for m=1:length(temp1{l,1})
            if length(temp1{l,1})<3              
            ind4=strfind(vow,temp1{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp1{l,1}(m));
                    consb(indc,ind4)=consb(indc,ind4)+1;
                else
                    vowf(indv,ind4)=vowf(indv,ind4)+1;                    
                end  
            end    
           
        end 
        
        
    end    
    vowf(indv,19)=vowf(indv,19)+length(temp1);
    consb(indc,29)=consb(indc,29)+length(temp1);
    end
    
    
    if ~isempty(temp2)
    for l=1:length(temp2)
        if length(temp2{l,1})>2
            vowf(indv,16)=vowf(indv,16)+1;
            consb(indc,26)=consb(indc,26)+1;
            if ~isempty(strfind(temp2{l,1},name{i,4}(1))) && ~isempty(strfind(temp2{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consb(indc,ind7)=consb(indc,ind7)+1;
                    vowf(indv,ind6)=vowf(indv,ind6)+1; 
                    
                else
                    vowfs{indv,1}=[vowfs{indv,1} ' ' num2str(i)];
                    consbs{indc,1}=[consbs{indc,1} ' ' num2str(i)];
            end
        elseif length(temp2{l,1})<2
            vowf(indv,17)=vowf(indv,17)+1;
            consb(indc,27)=consb(indc,27)+1;
        else    
            ind3=strfind(vow,temp2{l,1}(1));
            if isempty(ind3)
                vowf(indv,18)=vowf(indv,18)+1;
                consb(indc,28)=consb(indc,28)+1;
            end
        end  
        
        for m=1:length(temp2{l,1})
            if length(temp2{l,1})<3
                    
            
            ind4=strfind(vow,temp2{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp2{l,1}(m));
                    consb(indc,ind4)=consb(indc,ind4)+1;
                else
                    vowf(indv,ind4)=vowf(indv,ind4)+1;                    
                end    
            end
        end 
        
        
    end    
    vowf(indv,19)=vowf(indv,19)+length(temp2);
    consb(indc,29)=consb(indc,29)+length(temp2);
    end
    
    if ~isempty(temp3)
    for l=1:length(temp3)
        if length(temp3{l,1})>2
            vowf(indv,16)=vowf(indv,16)+1;
            consb(indc,26)=consb(indc,26)+1;
            if ~isempty(strfind(temp3{l,1},name{i,4}(1))) && ~isempty(strfind(temp3{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consb(indc,ind7)=consb(indc,ind7)+1;
                    vowf(indv,ind6)=vowf(indv,ind6)+1; 
                    
                else
                    vowfs{indv,1}=[vowfs{indv,1} ' ' num2str(i)];
                    consbs{indc,1}=[consbs{indc,1} ' ' num2str(i)];
            end 
        elseif length(temp3{l,1})<2
            vowf(indv,17)=vowf(indv,17)+1;
            consb(indc,27)=consb(indc,27)+1;
        else    
            ind3=strfind(vow,temp3{l,1}(1));
            if isempty(ind3)
                vowf(indv,18)=vowf(indv,18)+1;
                consb(indc,28)=consb(indc,28)+1;
            end
        end  
        
        for m=1:length(temp3{l,1})
            if length(temp3{l,1})<3
                   
           
            ind4=strfind(vow,temp3{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp3{l,1}(m));
                    consb(indc,ind4)=consb(indc,ind4)+1;
                else
                    vowf(indv,ind4)=vowf(indv,ind4)+1;                    
                end 
            end     
           
        end 
        
        
    end    
    vowf(indv,19)=vowf(indv,19)+length(temp3);
    consb(indc,29)=consb(indc,29)+length(temp3);
    end
    
    if ~isempty(temp4)
    for l=1:length(temp4)
        if length(temp4{l,1})>2
            vowf(indv,16)=vowf(indv,16)+1;
            consb(indc,26)=consb(indc,26)+1;
            if ~isempty(strfind(temp4{l,1},name{i,4}(1))) && ~isempty(strfind(temp4{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consb(indc,ind7)=consb(indc,ind7)+1;
                    vowf(indv,ind6)=vowf(indv,ind6)+1; 
                    
                else
                    vowfs{indv,1}=[vowfs{indv,1} ' ' num2str(i)];
                    consbs{indc,1}=[consbs{indc,1} ' ' num2str(i)];
           end 
        elseif length(temp4{l,1})<2
            vowf(indv,17)=vowf(indv,17)+1;
            consb(indc,27)=consb(indc,27)+1;
        else    
            ind3=strfind(vow,temp4{l,1}(1));
            if isempty(ind3)
                vowf(indv,18)=vowf(indv,18)+1;
                consb(indc,28)=consb(indc,28)+1;
            end
        end  
        
        for m=1:length(temp4{l,1})
            if length(temp4{l,1})<3
                   
            
            ind4=strfind(vow,temp4{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp4{l,1}(m));
                    consb(indc,ind4)=consb(indc,ind4)+1;
                else
                    vowf(indv,ind4)=vowf(indv,ind4)+1;                    
                end 
            end    
           
        end 
        
        
    end    
    vowf(indv,19)=vowf(indv,19)+length(temp4);
    consb(indc,29)=consb(indc,29)+length(temp4);
    end
    
else
    
    %Consonant front Vowel back
    indv=strfind(vow,name{i,4}(2));
    indc=strfind(cons,name{i,4}(1));
    
    %Settle the flags
    
    if ~isempty(temp1)
    for l=1:length(temp1)
        if length(temp1{l,1})>2
            vowb(indv,16)=vowb(indv,16)+1;
            consf(indc,26)=consf(indc,26)+1;
            if ~isempty(strfind(temp1{l,1},name{i,4}(1))) && ~isempty(strfind(temp1{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consf(indc,ind7)=consf(indc,ind7)+1;
                    vowb(indv,ind6)=vowb(indv,ind6)+1; 
                    
                else
                    vowbs{indv,1}=[vowbs{indv,1} ' ' num2str(i)];
                    consfs{indc,1}=[consfs{indc,1} ' ' num2str(i)];
           end 
        elseif length(temp1{l,1})<2
            vowb(indv,17)=vowb(indv,17)+1;
            consf(indc,27)=consf(indc,27)+1;
        else    
            ind3=strfind(cons,temp1{l,1}(1));
            if isempty(ind3)
                vowb(indv,18)=vowb(indv,18)+1;
                consf(indc,28)=consf(indc,28)+1;
            end
        end  
        
        for m=1:length(temp1{l,1})
            if length(temp1{l,1})<3
                   
            
            ind4=strfind(vow,temp1{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp1{l,1}(m));
                    consf(indc,ind4)=consf(indc,ind4)+1;
                else
                    vowb(indv,ind4)=vowb(indv,ind4)+1;                    
                end 
            end    
           
        end 
        
        
    end    
    vowb(indv,19)=vowb(indv,19)+length(temp1);
    consf(indc,29)=consf(indc,29)+length(temp1);
    end
    
    if ~isempty(temp2)
    for l=1:length(temp2)
        if length(temp2{l,1})>2
            vowb(indv,16)=vowb(indv,16)+1;
            consf(indc,26)=consf(indc,26)+1;
            if ~isempty(strfind(temp2{l,1},name{i,4}(1))) && ~isempty(strfind(temp2{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consf(indc,ind7)=consf(indc,ind7)+1;
                    vowb(indv,ind6)=vowb(indv,ind6)+1; 
                    
                else
                    vowbs{indv,1}=[vowbs{indv,1} ' ' num2str(i)];
                    consfs{indc,1}=[consfs{indc,1} ' ' num2str(i)];
            end 
        elseif length(temp2{l,1})<2
            vowb(indv,17)=vowb(indv,17)+1;
            consf(indc,27)=consf(indc,27)+1;
        else    
            ind3=strfind(cons,temp2{l,1}(1));
            if isempty(ind3)
                vowb(indv,18)=vowb(indv,18)+1;
                consf(indc,28)=consf(indc,28)+1;
            end
        end  
        
        for m=1:length(temp2{l,1})
            if length(temp2{l,1})<3
                   
            
            ind4=strfind(vow,temp2{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp2{l,1}(m));
                    consf(indc,ind4)=consf(indc,ind4)+1;
                else
                    vowb(indv,ind4)=vowb(indv,ind4)+1;                    
                end  
            end    
           
        end 
        
        
    end    
    vowb(indv,19)=vowb(indv,19)+length(temp2);
    consf(indc,29)=consf(indc,29)+length(temp2);
    end
    
    if ~isempty(temp3)
    for l=1:length(temp3)
        if length(temp3{l,1})>2
            vowb(indv,16)=vowb(indv,16)+1;
            consf(indc,26)=consf(indc,26)+1;
            if ~isempty(strfind(temp3{l,1},name{i,4}(1))) && ~isempty(strfind(temp3{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consf(indc,ind7)=consf(indc,ind7)+1;
                    vowb(indv,ind6)=vowb(indv,ind6)+1; 
                    
                else
                    vowbs{indv,1}=[vowbs{indv,1} ' ' num2str(i)];
                    consfs{indc,1}=[consfs{indc,1} ' ' num2str(i)];
            end  
        elseif length(temp3{l,1})<2
            vowb(indv,17)=vowb(indv,17)+1;
            consf(indc,27)=consf(indc,27)+1;
        else    
            ind3=strfind(cons,temp3{l,1}(1));
            if isempty(ind3)
                vowb(indv,18)=vowb(indv,18)+1;
                consf(indc,28)=consf(indc,28)+1;
            end
        end  
        
        for m=1:length(temp3{l,1})
            if length(temp3{l,1})<3
                  
            
            ind4=strfind(vow,temp3{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp3{l,1}(m));
                    consf(indc,ind4)=consf(indc,ind4)+1;
                else
                    vowb(indv,ind4)=vowb(indv,ind4)+1;                    
                end  
            end    
           
        end 
        
        
    end    
    vowb(indv,19)=vowb(indv,19)+length(temp3);
    consf(indc,29)=consf(indc,29)+length(temp3);
    end
    
    if ~isempty(temp4)
    for l=1:length(temp4)
        if length(temp4{l,1})>2
            vowb(indv,16)=vowb(indv,16)+1;
            consf(indc,26)=consf(indc,26)+1;
            if ~isempty(strfind(temp4{l,1},name{i,4}(1))) && ~isempty(strfind(temp4{l,1},name{i,4}(2)))
                    ind6=strfind(vow,name{i,4}(1));
                    ind7=strfind(cons,name{i,4}(2));
                    consf(indc,ind7)=consf(indc,ind7)+1;
                    vowb(indv,ind6)=vowb(indv,ind6)+1; 
                    
                else
                    vowbs{indv,1}=[vowbs{indv,1} ' ' num2str(i)];
                    consfs{indc,1}=[consfs{indc,1} ' ' num2str(i)];
            end
        elseif length(temp4{l,1})<2
            vowb(indv,17)=vowb(indv,17)+1;
            consf(indc,27)=consf(indc,27)+1;
        else    
            ind3=strfind(cons,temp4{l,1}(1));
            if isempty(ind3)
                vowb(indv,18)=vowb(indv,18)+1;
                consf(indc,28)=consf(indc,28)+1;
            end
        end  
        
        for m=1:length(temp4{l,1})
            if length(temp4{l,1})<3
                    
            
            ind4=strfind(vow,temp4{l,1}(m));
                if isempty(ind4)
                    ind4=strfind(cons,temp4{l,1}(m));
                    consf(indc,ind4)=consf(indc,ind4)+1;
                else
                    vowb(indv,ind4)=vowb(indv,ind4)+1;                    
                end    
            end    
           
        end 
        
        
    end    
    vowb(indv,19)=vowb(indv,19)+length(temp4);
    consf(indc,29)=consf(indc,29)+length(temp4);
    end
    
end 
clear temp1;clear temp2;clear temp3;clear temp4;clear indv;clear indc;
end


prin1(1,:)=vow1;
for i=2:16
    for j=1:21
        if j==1
            prin1(i,1)=vow1(1,i);
        elseif j==21
            prin1(i,j)={vowfs{i-1,1}};
        else 
            if vowf(i-1,j-1)==0
                prin1(i,j)={''};
            else    
                prin1(i,j)={num2str(vowf(i-1,j-1))};
            end    
        end
    end
end   

prin2(1,:)=vow1;
for i=2:16
    for j=1:21
        if j==1
            prin2(i,1)=vow1(1,i);
        elseif j==21
            prin2(i,j)={vowbs{i-1,1}};    
        else
            if vowb(i-1,j-1)==0
                prin2(i,j)={''};
            else 
                prin2(i,j)={num2str(vowb(i-1,j-1))};
            end    
        end
    end
end  

prin3(1,:)=cons1;
for i=2:26
    for j=1:31
        if j==1
            prin3(i,1)=cons1(1,i);
        elseif j==31
            prin3(i,j)={consfs{i-1,1}};    
        else
            if consf(i-1,j-1)==0
                prin3(i,j)={''};
            else    
                prin3(i,j)={num2str(consf(i-1,j-1))};
            end    
        end
    end
end  

prin4(1,:)=cons1;
for i=2:26
    for j=1:31
        if j==1
            prin4(i,1)=cons1(1,i);
        elseif j==31
            prin4(i,j)={consbs{i-1,1}};    
        else
            if consb(i-1,j-1)==0
                prin4(i,j)={''};
            else    
                prin4(i,j)={num2str(consb(i-1,j-1))};
            end    
        end
    end
end  

oname=['CM/',sname,'.mat']; %On linux, save the data in a file CM/sname.mat
save(oname,'prin1','prin2','prin3','prin4')

%oname=['CM/',sname,'.xls'];
%xlswrite(oname,prin1,1,'A1:U16');
%xlswrite(oname,prin2,2,'A16:U16');
%xlswrite(oname,prin3,3,'A1:AE26');
%xlswrite(oname,prin4,4,'A1:AE26');


 end % loop over children
