MATLAB: How to assign the colorbar for a subimage in a figure in MATLAB 7.8 (R2009a)

colorbarcolormapMATLABsubimage

I would like to assign a colorbar to a subimage in the figure and have it indexed by this range of the map.

Best Answer

The colorbar can be attributed to a subimage in the figure using the colormap function. To set the range of the colormap indexed by the colorbar, set its 'YLim' property to this range.
The following code illustrates this concept:
load trees
subplot(1,2,1);
subimage(X,map);
subplot(1,2,2);
subimage(X,winter(64));
fullmap = [map;winter]; %map and winter for second image
colormap(fullmap); %set colormap
colorbar('YLim',[size(map,1), size(map,1)+64]); %colorbar's ylimits correspond to winter