Get current colormap used on i.e. surfaceplot as string

조회 수: 13 (최근 30일)
dm
dm 2011년 5월 8일
Is there any way that I can get the current colormap of i.e. a surface plot returned as a string?
If I have a plot, e.g.
x = -3:0.1:3;
y = -3:0.1:3;
[X Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
Z = abs(cos(sinc(R)).^X);
h = surf(X,Y,Z);
colormap('cool');
I can get access to the current colormap through
cm = get(gcf,'Colormap');
which is represented as a 64-by-3) double array. However, I'd like to get the name of the colormap used returned as a string (i.e. s = 'cool'); is there any way to do this - so I later on can use this string in a strcmpi() check?
best regards, dm

채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 8일
You could figure out the number of entries in the current colormap, and then loop through all the named colormaps, invoking them to return a map of the appropriate size, and then using isequal() to compare the two. For this purpose keep in mind that colormap names are actually functions that, when invoked, return an Mx3 matrix of values.
For example,
nent = size(cm,1);
cmaps = {@cool, @hot, @copper, @flag};
for K = 1:length(cmaps)
cmap = cmaps{K}(nent);
if isequal(cm, cmap)
%found a map
break
end
end
You will have the problem that any Mx3 of the appropriate class and data range can be a colormap: colormaps are not restricted to the named ones.
If you were hoping that MATLAB remembered the name of the colormap in use... sorry, it doesn't do that.
  댓글 수: 1
dm
dm 2011년 5월 9일
Thanks for the answer. I was indeed hoping MATLAB would remember the name of the colormap in use, so things would turn out easier in my script, but it isn't a big issue. I already use something similar to your hack, but I have to say yours is much more elegant - so I might have to steal your piece of code and put a thanks to in file! ;)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by