-1

I am trying to save multiple png files of circles with different colours in MATLAB. I wish to have multiple files with the name:

RINGS_Base_<n><colorname> 

n being the number of different files displayed and colorname being the name of the colour of the circle.

Since the circles have an RGB colour, I transformed each RGB colour into the actual name of the color by using the function translatecolor.

Can I call that function when I am naming each one of my files? If not, how can I name all of the files with their respective colours?

Thank you in advance for your help.

Here is my code:

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
Suever
  • 64,497
  • 14
  • 82
  • 101
Mraquel
  • 23
  • 8
  • Um... yes? Is there an actual problem here? It looks like you're already doing things (at least mostly) right... – tmpearce Mar 07 '17 at 13:49
  • I have the following error that I am having trouble to understand @tmpearce :>> RIGNSGenerator SWITCH expression must be a scalar or character vector constant. Error in RIGNSGenerator>translatecolor (line 18) switch(in) Error in RIGNSGenerator (line 12) print (strcat ('/Users/Stimuli_Rings/RINGS_Circle_', num2str(n),translatecolor(col1), '.png'), '-dpng') %strcat is to combining strings together – Mraquel Mar 07 '17 at 13:53

1 Answers1

2

Your issue is with the translatecolor function because the case statements of the switch statement cannot be arrays of numbers. Rather than using a switch statement, you could just use a sort of lookup table which relies upon having each value in a separate row and the corresponding strings in a cell array. You can then use ismember (with the 'rows' option) to figure out which value corresponds to the input and then use the result to index into the array of color names.

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')};
Suever
  • 64,497
  • 14
  • 82
  • 101
  • I keep having the following error and I don't know how to address it: Error using ismember>ismemberR2012a (line 182) Inputs A and B must be matrices with the same number of columns in the 'rows' case. Error in ismember (line 116) lia = ismemberR2012a(A,B,logical(flaginds(1))); – Mraquel Mar 07 '17 at 15:08
  • @Mraquel Ah I see now, `in` was a column vector in your data. I've updated the code to force `in` to be a row vector, allowing the appropriate comparison. – Suever Mar 07 '17 at 15:11
  • Thank you so much @Suever ! – Mraquel Mar 07 '17 at 15:17