I have a grayscle image. I can create a heatmap in matlab using:
I = imagesc(I);
it displays the intensity values in color. I want to get a color image using this heatmap. How can I do it?
This is the output of imagesc:
Presumably by "color image" you mean an RGB or truecolor image (an image array with three color channels). And what you're calling a "heatmap" is the colormap that Matlab applies by default to grayscale images (image arrays with only one color channel). A grayscale image plus a color map is referred to as an indexed color image (read more about that here). You can use the ind2rgb function to convert from indexed color to RGB:
IMG_gray = rand(100);
map = colormap; % Get the current colormap
IMG_rgb = ind2rgb(IMG_gray,map);
Note that by default the colormaps in Matlab use only 64 colors rather 256. To get smoother color gradations you can set the colormap manually via map = colormap(jet(256)); or map = colormap(hsv(256));.
If you then want an image file, you can use imwrite, which can take either RGB images or indexed color images with colormaps as input.