필터 지우기
필터 지우기

Transfer character string from sprintf to subtitle

조회 수: 2 (최근 30일)
Dennis Premoli
Dennis Premoli 2021년 5월 23일
댓글: Star Strider 2021년 5월 23일
Hi,
so I have I'm trying to create a character string with data dynamically inserted within it, so I'm using an sprintf
alloy_comps{1,1} = sprintf('{%\bfNi}-%.1f {%\bfCo}-%.1f {%\bfAl}-%.1f {%\bfTi}-%.1f {%\bfCr}-%.1f (at.%%)',Q{i,2},Q{i,3},Q{i,4},Q{i,5},Q{i,6})
I'm then passing this string onto the subtitle function as such
txt = alloy_comps{1,1};
subtitle(t,txt)
I then noticed that the formatting included in the sprintf wasn't being passed properly onto the subtitle function.
So, after a bit of trial and error I've realised that the sprintf stores/outputs this
'fNi}-18.8 fCo}-56.2 fAl}-2.5 fTi}-7.5 fCr}-15.0 (at.%)'
instead of this, which is what subtitle can properly read.
'{\bfNi}-18.8 {\bfCo}-56.2 {\bfAl}-2.5 {\bfTi}-7.5 {\bfCr}-15.0 (at.%%)'
and its essentially a case of garbage in and garbage out.
I'm not sure how to get around this issue, since sprintf doesn't se to recognise the TeX Markup '\bf' and reads only the '\b' which in turn breaks the whole thing.
Ideally I'd be avoiding this
and getting this
but with the data in it as well.
Just can't seem to find a way to dynamically pass both formatting and variable, at least not with sprintf cannibilizing half of it.

채택된 답변

Star Strider
Star Strider 2021년 5월 23일
To print a single ‘\’ using sprintf (and the others, like fprintf and compose), use a double backslant ‘\\’.
So:
i = 1;
Q = num2cell(rand(1,6));
alloy_comps{1,1} = sprintf('{\\bfNi}-%.1f {\\bfCo}-%.1f {\\bfAl}-%.1f {\\bfTi}-%.1f {\\bfCr}-%.1f (at.%%)',Q{i,2},Q{i,3},Q{i,4},Q{i,5},Q{i,6})
alloy_comps = 1×1 cell array
{'{\bfNi}-0.9 {\bfCo}-0.6 {\bfAl}-0.6 {\bfTi}-0.3 {\bfCr}-0.8 (at.%)'}
txt = alloy_comps{1,1};
figure
plot((1:10), rand (1,10), 'gp', 'MarkerFaceColor','g')
title('Combined EDX Plots for 151a')
subtitle(txt)
.
  댓글 수: 2
Dennis Premoli
Dennis Premoli 2021년 5월 23일
Now I'm kinda ashamed ahah
I knew about this just didn't occur to me to use it in this case.
Thank you very much!!
Star Strider
Star Strider 2021년 5월 23일
AAs always, my pleasure!
Please don’t be ashamed — just be willing to experiment!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by