
clc;clear;
subject={'Alina','Angela','Anton','Bob','Carly','Edward','Evan','Joanna','Latisha','Laura','Miguel','Norene','Shauna','Teddy','Tony'};
total_n_notequal=0;
total_diff=0;
n_cmpared=0;
for m=1:length(subject)
    sname = subject{m};
    
    aname=['DataNSCM_',sname];
    fname=['DataNSCM/DataNSCM_',sname];
    
    x=struct2cell(load(fname,aname));
    name=x{1}; %dereference the 1x1 cell array
    clear x disagreement_lst;
    
    differences=1;
    n_notequal=0;
    disagreement_lst = cell(1,3);
    disagreement_lst{1,1}='Examiner';
    disagreement_lst{1,2}='Transcriber';
    disagreement_lst{1,3}='Line';
    disagreement_lst{1,4}='T1/T2';
    
    if m==10 % Laura's data format is different
        startrow=1;
        col_examiner = 4;
        col_transcriber1 = 5;
        col_transcriber2 = 6;
    else
        startrow=2;
        col_examiner = 5;
        col_transcriber1 = 6;
        col_transcriber2 = 7;
    end
    
    for i=startrow:length(name)
        if ~isnan(name{i,col_transcriber1})
            n_cmpared = n_cmpared + 1;
            if strcmp(name{i,col_examiner},name{i,col_transcriber1}) == 0
                n_notequal = n_notequal+1;
                for k=1:min(length(name{i,col_examiner}),length(name{i,col_transcriber1}))
                    if name{i,col_examiner}(k) ~= name{i,col_transcriber1}(k)
                        differences=differences+1;
                        disagreement_lst{differences,1}=name{i,col_examiner}(k);
                        disagreement_lst{differences,2}=name{i,col_transcriber1}(k);
                        disagreement_lst{differences,3}=i;
                        disagreement_lst{differences,4}='T1';
                    end
                end
                if length(name{i,col_examiner}) > length(name{i,col_transcriber1})
                    differences=differences+1;
                    disagreement_lst{differences,1}=name{i,col_examiner}(length(name{i,col_examiner}));
                    disagreement_lst{differences,2}='';
                    disagreement_lst{differences,3}=i;
                    disagreement_lst{differences,4}='T1';
                elseif length(name{i,col_examiner}) < length(name{i,col_transcriber1})
                    differences=differences+1;
                    disagreement_lst{differences,1}='';
                    disagreement_lst{differences,2}=name{i,col_transcriber1}(length(name{i,col_transcriber1}));
                    disagreement_lst{differences,3}=i;
                    disagreement_lst{differences,4}='T1';
                end
            end
        end
        if ~isnan(name{i,col_transcriber2})
            n_cmpared = n_cmpared + 1;
            if strcmp(name{i,col_examiner},name{i,col_transcriber2}) == 0
                n_notequal = n_notequal+1;
                for k=1:min(length(name{i,col_examiner}),length(name{i,col_transcriber2}))
                    if name{i,col_examiner}(k) ~= name{i,col_transcriber2}(k)
                        differences=differences+1;
                        disagreement_lst{differences,1}=name{i,col_examiner}(k);
                        disagreement_lst{differences,2}=name{i,col_transcriber2}(k);
                        disagreement_lst{differences,3}=i;
                        disagreement_lst{differences,4}='T2';
                    end
                end
                if length(name{i,col_examiner}) > length(name{i,col_transcriber2})
                    differences=differences+1;
                    disagreement_lst{differences,1}=name{i,col_examiner}(length(name{i,col_examiner}));
                    disagreement_lst{differences,2}='';
                    disagreement_lst{differences,3}=i;
                    disagreement_lst{differences,4}='T2';
                elseif length(name{i,col_examiner}) < length(name{i,col_transcriber2})
                    differences=differences+1;
                    disagreement_lst{differences,1}='';
                    disagreement_lst{differences,2}=name{i,col_transcriber2}(length(name{i,col_transcriber2}));
                    disagreement_lst{differences,3}=i;
                    disagreement_lst{differences,4}='T2';
                end
            end
        end
    end
    differences=differences-1;
    total_n_notequal=total_n_notequal+n_notequal;
    total_diff=total_diff+differences;
    subject{m}
    disagreement_lst_summary = analyze_disagreement_lst(disagreement_lst, subject{m})
    plot_disagreement_lst(disagreement_lst_summary, subject{m});
    
    
end
precentage_notequal=total_n_notequal/n_cmpared
avg_diffs=total_diff/total_n_notequal