Cell Array storing RGB

20 次查看(过去 30 天)
SG
SG 2015-5-15
评论: SG 2015-5-15
I am storing color short names in a cell array as follows:
color{i,j} = 'r';
Then I'd like to use this cell array to set the color properties of some texts as follows:
text('String', d{i,j},'Units', 'Points',...
'FontSize', 8, 'HorizontalAlignment', 'right', ...
'Color', color(i,j));
However, I get the error "Color value must be a 3 element numeric vector". I have tried using a function that convert the short name of colors to their RGB triplet, but it seems that color(i,j) does not output a string/char. How should I go about this? Thank you.

采纳的回答

Walter Roberson
Walter Roberson 2015-5-15
If you are doing one at a time, then color{i,j} gets to the character.
If you were trying to do a whole bunch of them, such as to name the colors for each point of scatter3, then a lookup table is probably easiest.
RGBtab = [0 0 1; 0 1 0; 0 0 0; 1 0 0]; %RGB codes for colors
colorabbr = {'b', 'g', 'k', 'r'}; %must match order of RGB triple rows
[tf, idx] = ismember(color(:), colorabbr); %look up codes to find index
colorrgb = RGBtab(idx,:); %convert to RGB triples
and now colorrgb would be an N x 3 of RGB. If you needed to you could
reshape(colorrgb, size(color,1), size(color,2), 3)
to get a truecolor array

更多回答(1 个)

Image Analyst
Image Analyst 2015-5-15
That should work except that you used parentheses which gives the whole cell, rather than braces, which gives the contents of the cell.
color(i,j) = a cell, the cell in the ith row, jth column of the cell array called color.
color{i,j} = the CONTENTS OF the cell at row i, column j
Try this:
text('String', d{i,j},'Units', 'Points',...
'FontSize', 8, 'HorizontalAlignment', 'right', ...
'Color', color{i,j}); % use braces instead of parentheses.
See the FAQ for a good explanation of when to use braces or parentheses: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
  1 个评论
SG
SG 2015-5-15
That's even better, but I have already accepted an answer. I was convinced I had tried that, but looks like I hadn't. Thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by