MATLAB: Problem using ind2rgb and CDataMapping

cdatamappingimagescind2rgb

Hi, I have a matrix (M) with values ranging from 0 to 5000 that I want to save as an RGB image.
When plotting the image using imagesc(M,'cdatamapping','scaled'); i see exactly what i want.
However, using rgbM=ind2rgb(M,jet); (I tried different sized colormaps, didnt help)
rgbM now has only 2~3 colors and plotting it looks completely different.
I can't find help for this seemingly simple problem online… Any tips on how it should be done?

Best Answer

You need to call IND2RGB with a colormap that is as big as your data. Something like this:
M = round(5000*rand(300,300));
imagesc(M,'cdatamapping','scaled');
rgbM = ind2rgb(M - min(M(:)) + 1, jet(range(M(:))+1));
figure;
image(rgbM);