function stackederrorbar_plot(fig_n, subject, CV_ORDER, Pe_row, p, s_order, Hrow, Havg, Hall, sigma, cm_length, KEEP, VOWSTIM_LIST, CONSTIM_LIST, CONSTIM_LIST_ED)
%create stacked bar graph
cm_i=fig_n;
figure(fig_n);
bar( Pe_row( s_order, : ) * 100, 'stacked' )

%draw error bars which show the confidence intervals
hold on
x = 1:length(Pe_row(s_order));
errorbar(x-0.45,p*100, sigma*100, '.', 'Color', [0,0,0])
hold off
%create legends and lable abscissa
if cm_i < 3
    legend(VOWSTIM_LIST,'Location','NorthEastoutside','Fontsize',14);
    set(gca,'XTick',1:1:length(s_order),'XTickLabel',VOWSTIM_LIST(s_order),'Fontsize',16);
else
    if strcmp(subject, 'Edward')
        legend(CONSTIM_LIST_ED,'Location','NorthEastoutside','Fontsize',14);
    else
        legend(CONSTIM_LIST,'Location','NorthEastoutside','Fontsize',14);
    end
    set(gca,'XTick',1:1:length(s_order),'XTickLabel',CONSTIM_LIST(s_order),'Fontsize',16);
end
xlim([0, length(s_order)] + 0.5);
ylim([0, 120])

%lable ordinate
set(gca,'YTick',0:20:120,'YTickLabel',{0, 20, 40, 60, 80, 100, ''},'Fontsize',16);

%create figure title
if cm_i == 1
    title(sprintf('NSCM %s: Confusion of Vowels in Inital Position ', subject),'Fontsize',18);
elseif cm_i == 2
    title(sprintf('NSCM %s: Confusion of Vowels in Final Position ', subject),'Fontsize',18);
elseif cm_i == 3
    title(sprintf('NSCM %s: Confusion of Consonants in Inital Position ', subject),'Fontsize',18);
elseif cm_i == 4
    title(sprintf('NSCM %s: Confusion of Consonants in Final Position ', subject),'Fontsize',18);
end
txtxlabel = sprintf('Ranked-Order %s as Stimuli', CV_ORDER{cm_i});
xlabel(txtxlabel,'Fontsize',18);
ylabel('Sum of Confusions in P_e [%]','Fontsize',18);

% label bars
for x=1:cm_length;
    x2 = find(KEEP == x); % this translates the index in the original data set to the index in the reduced data set
    if isempty(x2)
        continue;
    end
    tok = s_order(x2);  % index translation, x along axis corresonds to a different token due to sorting
    % from the lowest to the highst entropy
    c = cumsum(Pe_row(tok,:)); % cumulative sum of row belonging to
    % corresponding entropy
    large_error = find(Pe_row(tok,:) > 0.10); % find errors > 10%
    ypos = c(large_error); % ypos (cumulative sum) for the large erros
    % write error prob for large errors on bars
    for j=1:length(large_error)
        y = ypos(j);
        if cm_i < 3
            txt = sprintf('%s', VOWSTIM_LIST{large_error(j)});
            %txt = sprintf('%2.0f%%', 100*Pe_row(tok, large_error(j)));
        else
            txt = sprintf('%s', CONSTIM_LIST{large_error(j)});
            %txt = sprintf('%2.0f%%', 100*Pe_row(tok, large_error(j)));
        end
        % Choose color for text (white or black)
        col = [0,0,0];
        if any([1,2,3,4, 15,16 17] - large_error(j) == 0)
            col = [1,1,1];
        end
        
        text(x2-0.25,y*100-(Pe_row(tok,large_error(j))*60), txt, 'rotation', 0, 'color', col);
        
    end
    
    % write entropy for each stimulus above the stacked bar
    if ~isnan(Hrow(tok))
        txt = sprintf('%1.3f', Hrow(tok));
    else
        txt = 'NO PRESENT';
    end
    if isnan(c(end))
        c(end) = 0;
    end
    
    text(x2, c(end)*100 + 7, txt, 'rotation', 90);
end

% Indicaate H_avg on plot
txt = sprintf('H_{avg} = %2.3f', Havg);
text(1.5, 110, txt, 'FontSize', 16);
% Indicaate H_overall on plot
txt = sprintf('H_{ovall} = %2.3f', Hall);
text(1.5, 100 , txt, 'FontSize', 16);
if strcmp(subject, 'Edward') && cm_i > 2
    text(1.5, 90 , '*r removed, since he cannot say r', 'FontSize', 16);
end

fname = sprintf('stackedbar_%s_%s.eps', subject, CV_ORDER{cm_i});
print(gcf, fullfile('graphics', fname), '-depsc2'); % print stacked barplot to eps