在matlab

时间:2017-03-07 13:44:52

标签: matlab function filenames suffix

我想在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

1 个答案:

答案 0 :(得分: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')};