MATLAB: How to change the size of a custom colormap that I have generated in MATLAB

colormapcolormapeditorcustomeditorhsvjetMATLABmatrixsize;

I have a custom colormap "mycmap" which can represent 64 colors. This colormap, stored in "mycolor.mat", is a 64×3 matrix specifying the RGB values for the colors. I would like to interpolate these color values and get a larger or smaller number of colors spanning the same range of colors.
The operation would be similar to how the COLORMAP function works when given an argument specifying a certain number of colors derived from one of the built-in colormaps such as "jet", "copper", or "hsv", e.g.:
cmap = colormap(jet(99));
returns a 99-by-3 version of the "jet" colormap.

Best Answer

You can write your own function in MATLAB to give a custom colormap a specific size. For example, the following function CUSTOMMAP generates a new colormap of up to 64 elements from custom colormap “mycmap”, stored in “mycolor.mat”:
function mycolormap = custommap(m)
load mycolor
cindex = linspace(1,64,m);
r = interp1([1:64],mycmap(:,1),cindex);
g = interp1([1:64],mycmap(:,2),cindex);
b = interp1([1:64],mycmap(:,3),cindex);
mycolormap = [r' g' b'];
This function when called with the syntax:
cmap_new = custommap(14)
returns a new colormap, "cmap_new", which represents 14 colors.