Change colors of individual strings of a figure

조회 수: 65 (최근 30일)
Nicolas Kaiser
Nicolas Kaiser 2020년 11월 20일
편집: dpb 2022년 6월 13일
Hello dear community,
I want to display the text of a figure in different colors. The text consists of several strings which are displayed with the font color black. Dynamically strings should be added to the text line by line with a fixed color if a boolean variable is true. Unfortunately I have no idea how this could work.
Here is an example how I have tried:
set(mainHandles.text, 'String', [{'abc'};...
{'dfg'};...
{'hij'};...
{'klm'};...
{'stu'};...
{'vwxyz'}]); %black font coulor
text_data = arrayfun(@(x) text(join(['\\color{x}sadfsdgs')),...
cell2mat(Data(is_true,1)), 'UniformOutput', false);
mainHandles.text.String = vertcat(mainHandles.text.String, text_modules);
mainHandles.text is the text object of the figure. Data contains the color of the respective string. With is_true the active strings are addressed.
Regardless of the fact that I have to reset the text of the figure with set, passing the font color in the string itself does not work - invalid parameter/value pair arguments.
Do any of you have an idea how I could make it all happen?
Many thanks for the help

채택된 답변

dpb
dpb 2020년 11월 20일
Well, it'll take a fair amount of detailed coding, but it can be done...as a crude illustration try the following code...
close,hAx=gca;
hTxt=text(0.5,0.5, [{'\color{black}abc'};...
{'\color{red}dfg'};...
{'\color{black}hij'};...
{'\color{black}klm'};...
{'\color{black}stu'};...
{'\color{darkGreen}vwxyz'}]);
pause 5
hTxt.String(1)=strrep(hTxt.String(1),'black','red');
  댓글 수: 3
Maurilio Matracia
Maurilio Matracia 2022년 6월 13일
Hi Nicolas, could you please attach the code you used for applying the rgb triplets?
dpb
dpb 2022년 6월 13일
편집: dpb 2022년 6월 13일
I have no idea what code may have used, but the syntax is
hTxt=text(0.1,0.5, {'\color[rgb]{0, 1, 0}abc'});
NB: the RGB string is inside square brackets and the RGB triplet is a cell array; the syntax is quite particular in this.
As noted (I think above?) this is all in the "See Also" doc link from text inside the text properties "TeX Markup Options" section.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by