MATLAB: How to set conditions (i.e. not linear between two values) for colormap levels (Custom Colormap)

colormap

I want to implement a conditional colormap depending on levels (i.e. value of z) similar to a traffic light (green=go, yellow=slow down, red=stop) in a 3D bar plot of an array of z values. The colormap should display green when 0<z=<0.2; yellow when 0<z=<0.4 and red when z>0.4. However, it plots the value linearly between red, yellow and green; what I need is the full color when z value is in the corresponding color range. I tried to work it out with caxis but it did not work. Someone knows how to input conditions for each level of the map? This is the code for the plot and the colormap.
b=bar3((DGP),1);
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
end
view(0,90)
shading interp
DGPmap = [1,0,0;1,1,0;0,1,0]; % red; yellow; green;
colormap(DGPmap);
Thank you for the help!

Best Answer

remove the "shading interp".
for k = 1:length(b)
zdata = b(k).ZData;
zbin = 1 + (z > 0) + (zdata > 0.2) + (zdata > 0.4); %(-inf, 0], (0, 0.2], (0.2, 0.4], (0.4, inf)
b(k).CData = zbin;
end
and add another colormap entry at the beginning of your table that has the color for the case z <= 0