Changing colour of title in plots

조회 수: 36 (최근 30일)
Jason
Jason 2018년 5월 3일
댓글: Star Strider 2018년 5월 4일
Hello, I am trying to change the colour of strings in my plot titles and have used the following
title('\color{magenta}Month1, \color{cyan}Month2, \color{red}Month3')
However, I now want the strings Month1, Month2 & Month3 to be on different lines. I have tried
title({'\color{magenta}Month1, \color{cyan}Month2, \color{red}Month3'})
but the syntax is wrong.
Regards Jason

채택된 답변

Star Strider
Star Strider 2018년 5월 3일
You simply need to tweak your existing title call. Put each as a separate string in a cell array:
title({'\color{magenta}Month1', '\color{cyan}Month2', '\color{red}Month3'})
This will do what you want.
  댓글 수: 10
Jason
Jason 2018년 5월 4일
Im really sorry but I wwas mistaken, its still not working when I use a name for a string.
folder =
'C:\images\BC_1.0um_1.6um (Cam530)'
its class is:
ans =
'char'
So using the suggestion:
t = {folder, 'Month2', 'Month1'};
c = {'magenta', 'cyan', 'red'};
for k1 = 1:numel(t)
tc{k1} = sprintf('\\color{%s}%s', c{k1}, t{k1});
end
title(tc)
I get:
Star Strider
Star Strider 2018년 5월 4일
The single ‘backslant’ (\) characters are causing the problem, and the underscore characters cause a problem with the interpreter. You need to add two additional strrep calls:
t = {'C:\images\BC_1.0um_1.6um (Cam530)', 'C:\images\BC_1.0um_1.6um (Cam531)', 'C:\images\BC_1.0um_1.6um (Cam532)'};
c = {'magenta', 'cyan', 'red'};
for k1 = 1:numel(t)
ts = strrep(t{k1}, '\','\\'); % Replace ‘\’ With ‘\\’
ts = strrep(ts, '_','\_'); % Replace ‘_’ With ‘\_’
tc{k1} = sprintf('\\color{%s}%s', c{k1}, ts);
end
title(tc)
Single backslant operators are interpreted as control characters in sprintf (and fprintf), and underscores as designating the next character as a subscript using the default TeX interpreter. Putting a backslant ahead of such characters will force them to be treated as literals.
Specifying 'Interpreter','none' turns off everything, so none of the control characters are interpreted. Two serial strrep calls are necessary because it will replace one but not both in a single call. Doing both replacements in one call results in two different outputs, each with a different replacement but neither with both.

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 5월 3일
Use \newline.
title('\color{magenta}Month1,\newline \color{cyan}Month2,\newline \color{red}Month3')

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by