在matlab中用函数后缀命名一个文件
Naming a file with a function suffix in matlab
我想在 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
您的问题与 translatecolor
函数有关,因为 switch
语句的 case
语句不能是数字数组。您可以不使用 switch
语句,而只使用一种查找 table ,它依赖于将每个值放在单独的行中,并将相应的字符串放在元胞数组中。然后,您可以使用 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')};
我想在 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
您的问题与 translatecolor
函数有关,因为 switch
语句的 case
语句不能是数字数组。您可以不使用 switch
语句,而只使用一种查找 table ,它依赖于将每个值放在单独的行中,并将相应的字符串放在元胞数组中。然后,您可以使用 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')};