function singlet(CMsi, CORV, nCORV,corvOrd, i, subject, sit)  %08292015 code factorization Jie

%%    
    cm = CMsi; % CMs 1-2 Cons, 3-4 Vows (odd Initial, even Final),
    % Cons Initial
    corvcm(1:nCORV,:) = cm(corvOrd,:); 
    N = sum(corvcm,2);  % Sum of the rows total of each occurance
    N = repmat(N,[1 length(CORV)]);
    pm = 100*(corvcm./N);
    corvcm(:,1:nCORV) = corvcm(:,corvOrd);
    pm(:,1:nCORV) = pm(:,corvOrd);
    figure;
    switch sit(3)
        case 'C'
            hs = bar(corvcm,'stacked'); 
        case 'P'
            hs = bar(pm,'stacked'); 
    end
    
    %---- Jie 08212015 labeling starts here
    hPatches = get(hs,'Children'); 
    try hPatches = cell2mat(hPatches); catch, end  
    xData = get(hPatches(1),'XData');
    xPos = xData(end,:) - 0.5;
    yData = get(hPatches,'YData');
    try yData = cell2mat(yData); catch, end
    barYs = yData(2:4:end,:);
    barValues = diff([zeros(1,size(barYs,2)); barYs]);
    barValues(bsxfun(@minus,barValues,sum(barValues))==0) = 0;  
%Jie 082615 adding the star starts here 
    yStar = barYs(end,:);
    xStar = xPos;
    %Jie 082515 cleaning starts here 
    sumbar = ones(size(barValues,1),1)*sum(barValues);
    barPercent = barValues./sumbar;
    barPercentStar = barPercent(1:size(barPercent)+1:end);%082615 
%Jie 082715  error vs entropy starts here
Pe = 1-barPercentStar;
Pe(isnan(Pe)) = 0;
% Pe = ones(nCORV,1)*Pe; 
h = barPercent;
Hp = -h.*log2(h);
v = isnan(Hp);
% Pe(v)=0;
Hp(v)=0;
Hp0 = sum(Hp); %comment out when we do with Hp
%Jie 082715  error vs entropy ends here
    barPercent(isnan(barPercent)) = 0;
    barPercentLine = barPercent;
    barPercent(barPercent<=0.04) = 0;
    barPercentStar(isnan(barPercentStar)) = 3;%082615
    barPercentStar(barPercentStar>=0.80) = 3;%082615
    %Jie 082515 cleaning ends here
    yPos = yData(1:4:end,:) + barValues/2;
    yPosline = yData(1:4:end,:) + barValues;
    yPosline(barPercentLine==0)      = []; 
    xPos = xPos(ones(1,size(yPos,1)),:);
    xPosline = xPos-0.3; xPosline(barPercentLine==0) = []; 
    xPos(barPercent==0)      = [];      
    labels = CORV(ones(1,size(yPos,1)),:)';
    yStar(barPercentStar==3) = [];%082615
    xStar(barPercentStar==3) = [];%082615
    hStar = text(xStar,yStar,'*');
    hwhiteline = text(xPosline(:),yPosline(:),'--','Color','white');
% Jie 082615 adding the star ends here
    labels(barPercent==0)      = [];  
    yPos(barPercent==0)      = [];  
    barValues(barPercent==0) = [];  % remove empty patches
    hText = text(xPos(:), yPos(:), labels);
    errorText = num2str(round(barValues(:)));
    hTextPercent = text(xPos(:),yPos(:)-3,errorText);
    set(hText, 'FontSize',8,'color','w','FontWeight','bold');  % letter
    set(hText(10:end-10), 'FontSize',8,'color','k','FontWeight','bold'); 
    set(hTextPercent, 'FontSize',8,'color','w','FontWeight','bold');
    set(hTextPercent(10:end-10), 'FontSize',8,'color','k','FontWeight','bold');  % percentage 091115
    %---- Jie 08212015 labeling ends here
    set(gca,'XTick',1:length(CORV),'XTickLabel',CORV,'FontSize',16)
    legend(CORV, 'Location', 'NorthEastOutside','FontSize',11)
% Jie 08312015 code factorization    
    switch sit(1)
        case 'C'
            TOK= 'Consonants';
        case 'V'
            TOK= 'Vowels';
    end
    
    switch sit(2)
        case 'I'
            LOC= 'Initial';
        case 'F'
            LOC= 'Final';
    end
    
    switch sit(3)
        case 'C'
            DATTYP= 'Count';
            ylim([0 65]);
        case 'P'
            DATTYP= 'Percentage';
            ylim([0 100]);
    end
    
    title([LOC,' ',TOK, ' for ' subject ' (',DATTYP,')'])
    xlabel(['Presented ', TOK]);ylabel([DATTYP,' of Responses'])

    
%     fname = sprintf('stack%s_%s.eps',sit,subject);
    fname = sprintf('stack%s_%s.png',sit,subject);
%     print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
%     print(gcf, fullfile('./', fname), '-depsc2');
    print(gcf, fullfile('./', fname), '-dpng');

    %082715 Jie Pe vs Hp
    switch sit
        case 'CIC' 
            figure(1000);
        case 'CIP' 
            figure(1001);
        case 'CFC' 
            figure(1002);        
        case 'CFP' 
            figure(1003);
        case 'VIC' 
            figure(1004);
        case 'VIP' 
            figure(1005);        
        case 'VFC' 
            figure(1006);
        case 'VFP' 
            figure(1007);
    end
    
    switch subject 

        case 'Anton'
            marker = 'At'; color = 'g';
        case 'Bob'
            marker = 'Bb'; color = 'g';
        case 'Carly'
            marker = 'Cl'; color = 'g';
        case 'Evan'
            marker = 'Ev'; color = 'g';
        case 'Joanna'
            marker = 'Jn'; color = 'g';
        case 'Miguel'
            marker = 'Mg'; color = 'g';
        case 'Alina' 
            marker = 'Al'; color = 'r';
        case 'Angela'
            marker = 'Ag'; color = 'r';
        case 'Edward'
            marker = 'Ed'; color = 'r';
        case 'Latisha'
            marker = 'Lt'; color = 'r';
        case 'Laura'
            marker = 'Lr'; color = 'r';
        case 'Norene'
            marker = 'Nr'; color = 'r';
        case 'Shauna'
            marker = 'Sn'; color = 'r';
        case 'Teddy'
            marker = 'Td'; color = 'r';
        case 'Tony'
            marker = 'Tn'; color = 'r';
    end
%    marker
    ylim([0,4]);
text(Pe(:),Hp0(:),marker,'Color',color);hold on; title(sit); %08312015 Jie Code Factorization
%     fname = sprintf('%s_sum.eps',sit);
        fname = sprintf('%s_sum.png',sit);
%     print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
%     print(gcf, fullfile('./', fname), '-depsc2');
    print(gcf, fullfile('./', fname), '-dpng');
