MATLAB: Set color of each element in stacked barh

barhstacked bar

I have a series of events that I want to stack on top of each other in a timeline. There are two operators executing the events over the same timeline "Cole" and "Mele". I want to be able to set the color of the elements in the stacked barh according to the name of the event. For example, all events starting with "Col" (Col, Col1) should be the same color, but still differentiated by a black line if they occur right after each other. Similarly all events starting with "Mel" and so on. And then I'd like to be able to call out these events in the legend. Setting the 'facecolor' of the elements of h just sets the same numbered element in each bar to the same color. Any ideas?
Egr = 96;
Col1 = 45;
Col = 23.5;
Hib1_col = 378.5;
ConTra = 1;
Mel1 = 12;
Mel = 16;
Id1 = 44;
Id2 = 11.5;
Id3 = 7.5;
Hib1_mel = 362.5;
Cole = [Egr Col1 Col Col Col Col Col Col Col Hib1_col 0 0 0 0 0 0 0 0 0];
Mele = [Egr ConTra Id1 Mel1 Id2 Mel Id3 Mel Id3 Mel Id3 Mel Id3 Mel Id3 Mel Id3 Mel Hib1_mel];
names = {'CR'; 'MCR'};
A = [ 1 Cole
2 Mele];
figure();
h = barh(A(:,2:20), 'stacked');
set(gca,'ytick',[1:2],'yticklabel',names)
set(gca,'YDir','reverse')
grid on

Best Answer

This thread answered my question: https://www.mathworks.com/matlabcentral/answers/360292-how-can-i-change-the-color-of-each-stack-in-stacked-bar-plot