No BSD License  

Highlights from
coloindex

image thumbnail
from coloindex by alex sanchez
calculates the index for use with direct color indexing with multiple colormaps

colorindex(z,cmap,clim)
function ic = colorindex(z,cmap,clim)
% Calculates the index for direct index
% color mapping with multiple colormaps
%
% USAGE:
%   ic = colorindex(z,cmap,clim)
%
% DESCRIPTION:
%   Calculates the index for use with 
%   direct index color mapping
%   This allows for different colormaps
%   on the same figure.
%   This is done by appending new colormaps 
%   to the figure's colormap and saving the 
%   lengths of each subcolormap to calculate 
%   the corresponding color index.
%   The index can be used as follows
%   image(ic,'CdataMapping','Direct')
%   surf(ic,'CdataMapping','Direct')
%   contour(ic,'CdataMapping','Direct')
%   etc.
%   The only problem with this is that the 
%   matlab colobar function cannot be used.
%   Instead you can use colorbardi.m
%
% INPUT:
%   z  - Data
%   cmap - Colormap
%   clim - Pseudocolor axis scaling limits
%
% OUTPUT:
%   ic - Color Index
%
%Copy-Left, Alejandro Sanchez
hFig = get(0,'CurrentFigure');
if isempty(hFig)
   hFig = figure('Visible','Off');
end
if nargin<2
    cmap = get(hFig,'Colormap');
end
if nargin<3
    clim = [nanmin(z(:)),nanmax(z(:))];
end
n = size(cmap,1);
ud = get(hFig,'UserData');
N = 0;
if ~isfield(ud,'colmaplen')
    ud.colmaplen = n;
    MAP = cmap;
    set(hFig,'Colormap',MAP)
else
    MAP = get(hFig,'Colormap');
    nc = length(ud.colmaplen); %number of colormaps
    j = [0,cumsum(ud.colmaplen)];
    N = [];
    for k=1:nc %search for colormap
        if isequal(MAP(j(k)+1:j(k+1),:),cmap)
            N = j(k);
            break
        end
    end
    if isempty(N) %didn't find it, so add it
        ud.colmaplen(end+1) = n;
        N = size(MAP,1);
        MAP = [MAP; cmap];
        set(hFig,'Colormap',MAP)
    end
end
set(hFig,'UserData',ud,'Visible','On')
%calculate color index
% ic = fix((z-clim(1))/(clim(2)-clim(1))*(n-1))+N+1;
ic = fix((z-clim(1))/(clim(2)-clim(1))*n)+N+1; %I like this one better
ic(ic<N+1) = N+1; %fix ends
ic(ic>N+n) = N+n;
return

Contact us at files@mathworks.com