MATLAB: Set Colorbar TickLabels and TickMarks

colorbarcolormapticklabelstickmarksyticklabel

Hi all,
I am having trouble setting my colorbar ticklabels as I wish.
Specifically I have 8 points, and have created a colormap such that these 8 points get values from the darkest to the lightest color in the colormap. Similarly, I would like my colorbar's labels to follow the same pattern. Below is the code that I have written:
cmapdef = colormap(winter) ; %Define Colormap
cmap = cmapdef(1:8:end, :) ; %Find Values of colors corresponding to each point plotted
cbh = colorbar('YTickLabel', num2cell(1:8)) ;
However, the resulting colorbar has labels going as follows: 1 2 3 4 5 6 7 8 1 2 3. Instead I would expect the whole span of the color bar to be covered by 1 2 3 4 5 6 7 8.
What am I doing wrong?
Thanks in advance for your responses,
KMT.

Best Answer

Actually I managed to find a fix.
All the ticks range from 0 to 1, and they should have a tick label associated to them.
Therefore the following code solves the problem:
cmap = colormap(winter(8)) ; %Create Colormap
cbh = colorbar ; %Create Colorbar
cbh.Ticks = linspace(0, 1, 8) ; %Create 8 ticks from zero to 1
cbh.TickLabels = num2cell(1:8) ; %Replace the labels of these 8 ticks with the numbers 1 to 8