Get current colormap used on i.e. surfaceplot as string
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!