MATLAB: Bar chart colors depending on their value

barcolorcondition

(in MATLAB 2014a)
I would like to plot visuals to verify if a set of data complies with the requirements values.
req = [10,5,3];
data=[9,6,3];
To plot the following bar chart I used:
bar(req,0.5)
newXticklabel = {'label1','label2','label3'};
set(gca,'XtickLabel',newXticklabel);
hold on
bar(data,0.25,'g')
hold off
My question is: how do I change the color of one bar in red if the data is lower than the requirement value ?

Best Answer

Try this:
req = [10,5,3];
data=[9,6,3];
hb1 = bar(req,0.5);
newXticklabel = {'label1','label2','label3'};
set(gca,'XtickLabel',newXticklabel);
hold on
hb2 = bar(data,0.25,'g');
barcomp = hb2.YData < hb1.YData; % Logical Vector Identifying Bar
newbar = bar(hb2.XData(barcomp), hb2.YData(barcomp), hb2.BarWidth, 'r'); % Overplot In Red
hold off