MATLAB: How to set correctly the colorbar

colormapfigure

I tried to set a colorbar according to the following way:
m1 = [101 255 255]./255;
m2 = [207 170 85]./255;
m3 = [255 207 0]./255;
m4= [255 255 101]./255;
m5 = [203 0 203]./255;
map=[m1;m2;m3;m4;m5];
labels={'ET'; 'BSk'; 'BWh'; 'BWk'; 'Dfc'};
h=colorbar;
set(h,'YTickMode','manual','YTick',[1:length(map)],'YTickLabelMode','manual','YTickLabel',labels);
But in the figure only 'BWk' is marked in the midle of colorbar. How can I set correctly the colorbar?

Best Answer

A few problems. You don't want your y tick numbers to go from 1 to 5. You want them to go from 0 - 255 or between the min and max of your image. As it is, with no data displayed, your caxis (the range) is 0 to 1, yet your tick marks go from 1 to 5. So only the one at 1 will appear - the rest are off the top of the scale and not displayed or visible. See corrected code:
imshow('cameraman.tif');
m1 = [101 255 255]./255;
m2 = [207 170 85]./255;
m3 = [255 207 0]./255;
m4= [255 255 101]./255;
m5 = [203 0 203]./255;
map=[m1;m2;m3;m4;m5];
labels={'ET'; 'BSk'; 'BWh'; 'BWk'; 'Dfc'};
colormap(map); % Apply the colormap
h=colorbar;
set(h,'YTickMode','manual','YTickLabelMode','manual');
length(map)
cMapRange = caxis();
xTickNumbers = linspace(cMapRange(1), cMapRange(2), size(map, 1));
set(h,'YTick',xTickNumbers,'YTickLabel',labels);