MATLAB 获取颜色图名称列表
MATLAB Get a list of color map names
我正在编写一个 GUI,它将受益于用户按名称选择的颜色图。但是,我有点不知所措,因为我似乎无法以编程方式获取受支持的颜色映射名称列表!
虽然我可以硬编码名称;我的代码可能是 运行 在旧版本的 matlab 上可能有不同的颜色图。我主要关心的是 parula 颜色图,如果我没记错的话,它在 MATLAB 2014 中不存在。
有什么想法吗?
我不是 100% 确定它在 MATLAB 中工作正如@BillBokeey 在评论中指出的那样,这在 MATLAB 中不起作用,但在 Octave 中你可以使用:
CM = colormap('list');
它将return一个包含所有有效颜色图的字符串元胞数组。
CM =
{
[1,1] = autumn
[1,2] = bone
[1,3] = cool
[1,4] = copper
[1,5] = flag
[1,6] = gmap40
[1,7] = gray
[1,8] = hot
[1,9] = hsv
[1,10] = jet
[1,11] = lines
[1,12] = ocean
[1,13] = pink
[1,14] = prism
[1,15] = rainbow
[1,16] = spring
[1,17] = summer
[1,18] = white
[1,19] = winter
}
或者,您可以对它们进行硬编码,并在 if
语句上加上 graphicsversion(fhandle)
。
It returns returns true if the default graphics system is the old
handle graphics one.
您也可以尝试获取一个广泛的列表,然后检查 colormapname.m
是否是 matlabroot\toolbox\matlab\graph3d
中的文件。如果该函数存在,则颜色图会出现在该版本中。不过,您仍然需要对大量列表进行硬编码。
编辑: 正如@thewaywewalk 建议的那样,您可以在 matlabroot\toolbox\matlab\graph3d
中打开 Contents.m
并搜索 % Color maps.
它有一个列表版本中包含的颜色图。在 2014b 中,它位于第 29-48 行
获得大量可用颜色图的可能性:
在 matlabroot\help\matlab\ref
中,您可以找到文件名格式为 colormap_colormapname.png
的所有可用颜色图的预览
要获取列表,您可以使用:
CurrFolder=pwd;
cd(strcat(matlabroot,'\help\matlab\ref'))
Colormaps=dir('*colormap_*.png');
TmpColormapsList={Colormaps.name};
TmpColormapsList=cellfun(@(S)strrep(S,'colormap_',''),TmpColormapsList,'UniformOutput',false);
ColormapsList=cellfun(@(S)strrep(S,'.png',''),TmpColormapsList,'UniformOutput',false);
cd(CurrFolder);
这将输出包含可用颜色图名称的字符串元胞数组。
非常丑陋的 hack,但至少它适用于 2014b(如果您有另一个版本,请检查您的版本)
另一种方法(hack)可能是从 colormapeditor
函数中提取字符串:
colormapeditorString = fileread(strcat(matlabroot,'\toolbox\matlab\graph3d\colormapeditor.m'));
posStart = strfind(colormapeditorString,'stdcmap(maptype');
posEnd = strfind(colormapeditorString(posStart:end),'end') + posStart;
stdcmapString = colormapeditorString(posStart:posEnd);
split = strsplit(stdcmapString, '(mapsize)');
list = cellfun(@(x)x(find(x==' ', 1,'last'):end), split,'uni',0);
list(end) = [];
如果一切都失败了,您可以尝试回调例程:
function=ChangeCMap()
CMList=get(CMapList,'string'); %% Read colormap names
CMVal =get(CMapList,'value'); %% Get the index of desired colormap
try
colormap(Ax,CMList{CMVal}); %% Try to set the colormap...
catch Msg %% ... if it fails, then:
if strcmp(Msg.stack.name,'colormap') %% Check if error was caused by colormap function
set(Ax,'colormap`,'jet'); %% set "default" colormap (optional)
indices=1:length(CMList);
set(CMapList,'string',CMList{indices~=CMVal}) %% remove the colormap name thet caused error
else
disp(Msg) %% Print the error message in matlab shell
end
end
end
在此示例中,需要共享变量 CMapList
- 弹出菜单的句柄 - 和 Ax
- 轴的句柄。
调用该函数时,它会尝试设置颜色图。如果失败,它会设置默认颜色图并从菜单中删除有问题的名称。
确保第一个和最后一个颜色图不会导致错误,否则 CMapList
更新将不得不处理这些选项。
您还可以受益于每个颜色图都有自己的 .m
文件,因此您无需等到错误发生。
CMap='bone'; %% example
if exist(CMap,'file')
colormap(Ax,CMap) %% bone.m exist somewhere in the matlab paths
else
colormap(Ax,'jet') %% bone.m does not exist in the matlab paths
end
这提出了一点 - 您可以定义自己的颜色图并使算法生成丢失的 .m
文件...
鉴于 returns 一个 3 列矩阵的任何函数,或者甚至是一个包含 cmap
具有此类矩阵的变量的 .MAT 文件,都可以被 colormap
使用,要求“所有”颜色图的列表没有意义。
您可以使用 exist
(即 exist('parula', 'file')
)或 try
子句来检查某些函数是否存在,并在您的 GUI 中将它们作为颜色映射提供,尽管如果用户具有不生成颜色图的同名自定义函数,则可能会出现问题。
我有些用户喜欢制作完全自定义的颜色图,他们将这些颜色图保存在 MAT 文件中,因此对于他们来说,我会将颜色图制作为可自定义的文本字段,并进行一些验证以确保它确实是有效的颜色图。
我最近在应用生成器中构建应用时遇到了同样的问题。发现以上尝试过于繁琐后,我决定提供我的方法。
Matlab 将其颜色图存储为单个目录中的 *.m 文件,因此此目录中的文件名 select。
coldir = dir([matlabroot '\toolbox\matlab\graphics\color']);
maps={};
for i=1:length(coldir)
if coldir(i).isdir, continue, end
maps{end+1}=replace(coldir(i).name,'.m','');
end
我正在编写一个 GUI,它将受益于用户按名称选择的颜色图。但是,我有点不知所措,因为我似乎无法以编程方式获取受支持的颜色映射名称列表!
虽然我可以硬编码名称;我的代码可能是 运行 在旧版本的 matlab 上可能有不同的颜色图。我主要关心的是 parula 颜色图,如果我没记错的话,它在 MATLAB 2014 中不存在。
有什么想法吗?
我不是 100% 确定它在 MATLAB 中工作正如@BillBokeey 在评论中指出的那样,这在 MATLAB 中不起作用,但在 Octave 中你可以使用:
CM = colormap('list');
它将return一个包含所有有效颜色图的字符串元胞数组。
CM =
{
[1,1] = autumn
[1,2] = bone
[1,3] = cool
[1,4] = copper
[1,5] = flag
[1,6] = gmap40
[1,7] = gray
[1,8] = hot
[1,9] = hsv
[1,10] = jet
[1,11] = lines
[1,12] = ocean
[1,13] = pink
[1,14] = prism
[1,15] = rainbow
[1,16] = spring
[1,17] = summer
[1,18] = white
[1,19] = winter
}
或者,您可以对它们进行硬编码,并在 if
语句上加上 graphicsversion(fhandle)
。
It returns returns true if the default graphics system is the old handle graphics one.
您也可以尝试获取一个广泛的列表,然后检查 colormapname.m
是否是 matlabroot\toolbox\matlab\graph3d
中的文件。如果该函数存在,则颜色图会出现在该版本中。不过,您仍然需要对大量列表进行硬编码。
编辑: 正如@thewaywewalk 建议的那样,您可以在 matlabroot\toolbox\matlab\graph3d
中打开 Contents.m
并搜索 % Color maps.
它有一个列表版本中包含的颜色图。在 2014b 中,它位于第 29-48 行
获得大量可用颜色图的可能性:
在 matlabroot\help\matlab\ref
中,您可以找到文件名格式为 colormap_colormapname.png
要获取列表,您可以使用:
CurrFolder=pwd;
cd(strcat(matlabroot,'\help\matlab\ref'))
Colormaps=dir('*colormap_*.png');
TmpColormapsList={Colormaps.name};
TmpColormapsList=cellfun(@(S)strrep(S,'colormap_',''),TmpColormapsList,'UniformOutput',false);
ColormapsList=cellfun(@(S)strrep(S,'.png',''),TmpColormapsList,'UniformOutput',false);
cd(CurrFolder);
这将输出包含可用颜色图名称的字符串元胞数组。
非常丑陋的 hack,但至少它适用于 2014b(如果您有另一个版本,请检查您的版本)
另一种方法(hack)可能是从 colormapeditor
函数中提取字符串:
colormapeditorString = fileread(strcat(matlabroot,'\toolbox\matlab\graph3d\colormapeditor.m'));
posStart = strfind(colormapeditorString,'stdcmap(maptype');
posEnd = strfind(colormapeditorString(posStart:end),'end') + posStart;
stdcmapString = colormapeditorString(posStart:posEnd);
split = strsplit(stdcmapString, '(mapsize)');
list = cellfun(@(x)x(find(x==' ', 1,'last'):end), split,'uni',0);
list(end) = [];
如果一切都失败了,您可以尝试回调例程:
function=ChangeCMap()
CMList=get(CMapList,'string'); %% Read colormap names
CMVal =get(CMapList,'value'); %% Get the index of desired colormap
try
colormap(Ax,CMList{CMVal}); %% Try to set the colormap...
catch Msg %% ... if it fails, then:
if strcmp(Msg.stack.name,'colormap') %% Check if error was caused by colormap function
set(Ax,'colormap`,'jet'); %% set "default" colormap (optional)
indices=1:length(CMList);
set(CMapList,'string',CMList{indices~=CMVal}) %% remove the colormap name thet caused error
else
disp(Msg) %% Print the error message in matlab shell
end
end
end
在此示例中,需要共享变量 CMapList
- 弹出菜单的句柄 - 和 Ax
- 轴的句柄。
调用该函数时,它会尝试设置颜色图。如果失败,它会设置默认颜色图并从菜单中删除有问题的名称。
确保第一个和最后一个颜色图不会导致错误,否则 CMapList
更新将不得不处理这些选项。
您还可以受益于每个颜色图都有自己的 .m
文件,因此您无需等到错误发生。
CMap='bone'; %% example
if exist(CMap,'file')
colormap(Ax,CMap) %% bone.m exist somewhere in the matlab paths
else
colormap(Ax,'jet') %% bone.m does not exist in the matlab paths
end
这提出了一点 - 您可以定义自己的颜色图并使算法生成丢失的 .m
文件...
鉴于 returns 一个 3 列矩阵的任何函数,或者甚至是一个包含 cmap
具有此类矩阵的变量的 .MAT 文件,都可以被 colormap
使用,要求“所有”颜色图的列表没有意义。
您可以使用 exist
(即 exist('parula', 'file')
)或 try
子句来检查某些函数是否存在,并在您的 GUI 中将它们作为颜色映射提供,尽管如果用户具有不生成颜色图的同名自定义函数,则可能会出现问题。
我有些用户喜欢制作完全自定义的颜色图,他们将这些颜色图保存在 MAT 文件中,因此对于他们来说,我会将颜色图制作为可自定义的文本字段,并进行一些验证以确保它确实是有效的颜色图。
我最近在应用生成器中构建应用时遇到了同样的问题。发现以上尝试过于繁琐后,我决定提供我的方法。
Matlab 将其颜色图存储为单个目录中的 *.m 文件,因此此目录中的文件名 select。
coldir = dir([matlabroot '\toolbox\matlab\graphics\color']);
maps={};
for i=1:length(coldir)
if coldir(i).isdir, continue, end
maps{end+1}=replace(coldir(i).name,'.m','');
end