How do I create a colormap?

2 views (last 30 days)
Theo Kooijman
Theo Kooijman on 29 Jan 2014
Answered: Image Analyst on 29 Jan 2014
I've been having problems creating a colormap in the Staff version of Matlab (Matlab R2013b). I want to create a colormap of the type "jet", so I typed in the Command Window:
myColorMap = jet(256);
colormap(myColorMap);
colorbar
However, when I press enter, a window (called "Figure 1") pops up with a colormap, but with no picture in it. I've imported the picture (filename: Picture1Edit.jpg) into the program, I can see it in the sidebar "Current folder" left of my Command Window, so I know it's definetely there. Could anyone help me creating this colormap?

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 29 Jan 2014
That's the way you create a colormap. The colorbar command forces the creation of an axes, that forces the creation of a figure (unless the figure and an axes already exists). To display the image you'll have to use some of the image display-functions: imshow, image, imagesc; or even surf/pcolor if you want some more complex warping of the image. Then you can put a colorbar on the side of those displays.
HTH

Image Analyst
Image Analyst on 29 Jan 2014
Try this:
% Get full file name.
baseFileName = 'Picture1Edit.jpg';
folder = pwd; % Current folder
% Display if it exists, warn and exit if not.
if exist(fullFileName, 'file')
grayImage = imread(fullFileName);
imshow(grayImage);
else
message = sprintf('Error: %s not found', fullFileName);
uiwait(warndlg(message));
return;
end
% Apply colormap.
myColorMap = jet(256);
colormap(myColorMap);
colorbar

Categories

Find more on Color and Styling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!