MATLAB Answers

How can I change the size of a custom colormap that I have generated in MATLAB?

88 views (last 30 days)
I have a custom colormap "mycmap" which can represent 64 colors. This colormap, stored in "mycolor.mat", is a 64x3 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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
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.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!