How can I change the color of each text in a plot?

5 views (last 30 days)
Hello,
I'm trying to set the color of each text instance in a plot to a different value as shown:
C={[112 48 160], [85 142 213], [250 0 0], [146 208 80]}; %my color values
hcolors=cellfun(@(x) x/255,C,'UniformOutput',false)'; %colors converted to correct scale
set(h,{'Color'},hcolors) %sets the color of each line in h, this works fine
gtext=text(0.55*exes,maxes,{'Color'},hcolors);
However, when I try to set the color of the text according to the values in hcolors, I get the following error:
"Value cell array handle dimension must match handle vector length."
If I instead use
set(gtext,{'Color'},hcolors)
Everything works fine.
Is there a way to get the correct color output all in one line using text()?

Accepted Answer

Adam Danz
Adam Danz on 23 Aug 2019
Edited: Adam Danz on 23 Aug 2019
This line below should work iff the length of 'h' equal 4.
set(h,{'Color'},hcolors)
% iff
length(h) == 4
This line below, however, will definitely fail because it does not include any text values. Even when a cell array of string values is added, you cannot assign multiple colors. Multiple colors can be assigned, however, using the method above.
gtext=text(0.55*exes,maxes,{'Color'},hcolors); % will result in error
gtext=text(x,y,text); % this should work
set(gtext,{'Color'},hcolors)
  2 Comments
Sam Malamis
Sam Malamis on 23 Aug 2019
Edited: Sam Malamis on 23 Aug 2019
Thanks for the help, I accidentally left out the cell array of string values, the line should read
gtext=text(0.55*exes,maxes,txt,{'Color'},hcolors), where txt is the cell array of 4 string values.
Like you said, I still get the error, so I will use set() for the colors.
Adam Danz
Adam Danz on 23 Aug 2019
Edited: Adam Danz on 23 Aug 2019
Yep, that's the way to go!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!