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

5 ビュー (過去 30 日間)
Sam Malamis
Sam Malamis 2019 年 8 月 23 日
編集済み: Adam Danz 2019 年 8 月 23 日
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()?

採用された回答

Adam Danz
Adam Danz 2019 年 8 月 23 日
編集済み: Adam Danz 2019 年 8 月 23 日
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 件のコメント
Sam Malamis
Sam Malamis 2019 年 8 月 23 日
編集済み: Sam Malamis 2019 年 8 月 23 日
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 2019 年 8 月 23 日
編集済み: Adam Danz 2019 年 8 月 23 日
Yep, that's the way to go!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by