create colormap from matrix value

Dear all, I have matrix data with range of value [-15 to 34].
From that value I want that -9.99 show with white color and another value is degradation from red-white-blue.
How to do that ?
Any help will be great
Thanks

Answers (1)

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create sample data in the range of -15 to +34
oneLine = linspace(-15, 34, 800);
grayImage = repmat(oneLine, [600, 1]);
subplot(1, 2, 1);
imshow(grayImage, []);
title('The Image', 'fontSize', fontSize);
r = zeros(256, 1);
g = zeros(256, 1);
b = zeros(256, 1);
%Find index of -999
index999 = round((-9.999 - -15)/(34 - -15) * 256)
r(1:index999) = linspace(0, 1, index999);
g(1:index999) = linspace(0, 1, index999);
b(1:index999) = 1;
r(index999:end) = 1;
255-index999
g(index999:end) = linspace(1, 0, 257-index999);
b(index999:end) = linspace(1, 0, 257-index999);
subplot(1, 2, 2);
plot(r, 'r-', 'LineWidth', 9);
hold on;
plot(g, 'g-', 'LineWidth', 6);
plot(b, 'b-', 'LineWidth', 3);
grid on;
xlim([1,256]);
cmap = [r,g,b]; % jet(256);
colormap(cmap);
colorbar;
caxis([-15, 34]);
title('The Colormap', 'fontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

4 Comments

Thanks but I got problems when add your script into my script. this is all script that I use with additional script from your explanation. The problem is the value always changing every loop. Exactly how to fix it :( Thanks
% Script membuat animasi hasil tsunami
% Oleh Gugum Gumbira
%============================================================================
V = 60:60:2340; % identitas untuk buka data (dibuat pilihan)
C = cell(size(V)); % Jumlah cell sesuai V
for k = 1:numel(V) % Iterasi dari 1 sampai V (pemanggilan data)
filename = sprintf('ZA00%04d.TXT', V(k)); % load data yang dipanggil
C{k} = load(filename,'AAAA'); % Pemberian nama data yang dipanggil, data dalam bentuk cell
end
%=============================================================================
%=============Plot dan Animasi=====================================
figure(1) % Lokasi posting gambar (domain)
filename = 'testnew51.gif'; % Nama gif file
C = cell2mat(C); % convert data dari cell ke matriks
iwant1 = (1:708:27612) ; % matriks kolom1
iwant2 = (708:708:27612) ; % matriks kolom2
%===========================================
r = zeros(256, 1);
g = zeros(256, 1);
b = zeros(256, 1);
Z1=min(C(:));
Z2=max(C(:));
index999 = round((-9.9900 - Z1)/(Z2 - Z1) * 256)
r(1:index999) = linspace(0, 1, index999);
g(1:index999) = linspace(0, 1, index999);
b(1:index999) = 1;
r(index999:end) = 1;
255-index999
g(index999:end) = linspace(1, 0, 257-index999);
b(index999:end) = linspace(1, 0, 257-index999);
cmap = [r,g,b]; % jet(256);
colormap(cmap);
colorbar;
caxis([Z1, Z2]);
% [r c] = find(C==-9.9900);
%indices = find(C==-9.9900);
%C(indices)=0;
%Zlimit=[Z1 Z2];
%demcmap(Zlimit)
% colormapeditor
%[cmap]=buildcmap('bwr');
%colormap(cmap) % set colormap disini
%caxis ([-10 10])
for n = 1:length(iwant1) % looping sepanjang kolom1
contourf(flipud(flipud(flipud(C(:,iwant1(n):iwant2(n)))))) ; % Plot contour sesuai kolom
drawnow % update gambar per frame
frame = getframe(1); % mendapatkan gambar setiap frame
%F = getframe(gcf);
%image(F.cdata)
im = frame2im(frame); % convert frame ke image
[imind,cm] = rgb2ind(im,256); % convert RGB image ke indexceed image
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf); % image ke file grafik
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
Put it into a function where you send in the value for that iteration of the loop
whiteValue = whatever - depends on iteration......
cmap = GetColorMap(whiteValue);
colormap(cmap);
colorbar;
I am sorry not read the tutorial how to format question before post in this forum.
Do you mean to fix that problem I have to make function ?
Thanks
or you can leave it like that and just make sure whiteValue is the number that you say is changing on every iteration. Right now it says whiteValue = -9.999 because that's what you said. Just don't hard code the number in if it changes every iteration - use the value that's changing instead of using -9.999. It's a lot less messy to pass that number to a function and get the colormap back than having all those lines of code in your for loop.

Sign in to comment.

Tags

Asked:

on 2 May 2016

Edited:

on 2 May 2016

Community Treasure Hunt

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

Start Hunting!