-1

我正在尝试在 MATLAB 中保存多个具有不同颜色的圆圈的 png 文件。我希望有多个名称为:

RINGS_Base_<n><colorname> 

n是显示的不同文件的数量,colorname是圆圈颜色的名称。

由于圆圈具有 RGB 颜色,因此我使用函数 translatecolor 将每种 RGB 颜色转换为颜色的实际名称。

我可以在命名每个文件时调用该函数吗?如果没有,我怎样才能用它们各自的颜色命名所有文件?

预先感谢您的帮助。

这是我的代码:

RGB = [1 1 0; 1 0 1; 0 1 1; 1 0 0; 0 1 0; 0 0 1];

%
%RIGNSGenerator_FilledCircle1
%
n=1;
for col1 = transpose(RGB)
    FilledCircle1(2,2,5,300,transpose(col1)) %function []=FilledCircle1(x0,y0,Radius,N,col1)
    print (strcat ('/Users/Stimuli_Rings/RINGS_Circle_', num2str(n),translatecolor(col1), '.png'), '-dpng') %strcat is to combining strings together
n=n+1;
end 




function out=translatecolor(in) % translates the RGB colour into the actual name of the color
switch(in)
    case [1 1 0], out='yellow';
    case [1 0 1], out='pink';
    case [0 1 1], out='cyan';
    case [1 0 0], out='red'; 
    case [0 1 0], out='green';
    case [0 0 1], out='blue';
        return;
end
end
4

1 回答 1

2

您的问题在于该translatecolor函数,因为该case语句的switch语句不能是数字数组。而不是使用switch语句,您可以只使用一种查找表,该查找表依赖于将每个值放在单独的行中,并将相应的字符串放在单元格数组中。然后,您可以使用ismember(使用'rows'选项)来确定哪个值对应于输入,然后使用结果来索引颜色名称数组。

values = [1 1 0;
          1 0 1;
          0 1 1;
          1 0 0;
          0 1 0;
          0 0 1];

colors = {'yellow', 'pink', 'cyan', 'red', 'green', 'blue'};

out = colors{ismember(values, in(:).', 'rows')};
于 2017-03-07T14:08:59.393 回答