
% bare minimum - example -
clear all
close all
allLevelNames = {'one', 'two', 'three', 'four'};
levelValues = [1 2 3 4];
myMatrix = [3 3 3 3; 3 3 3 3; 4 4 1 1; 4 4 1 1];
% Now re-write colormap
aV = length(levelValues);
jj = jet(aV);
availableValues = unique(myMatrix);
getIndexes = arrayfun(@(xx) find(levelValues == xx), availableValues);
colormap(jj(getIndexes,:));
% Re-write myMatrix in the availableValues range
myMatrix = arrayfun(@(x) find(availableValues == x), myMatrix);
% Now Plot
h = imagesc(myMatrix);
% Handle if there is more then one color is in use
maxV = max(availableValues);
minV = min(availableValues);
if(length(availableValues) > 1) caxis([minV maxV]); end;
hcb=colorbar;
set(hcb, 'YTick', availableValues);
set(hcb, 'YTickLabel', allLevelNames(getIndexes));
set(gca,'YDir','normal') % Not to invert x-y but putting (0,0) on the bottom left
xlabel('x values');
ylabel('y values (care on (0,0) location from imagesc)');
title({['Variable colormap handling'];['by msuzen at gmail']});
No comments:
Post a Comment