Transfer character string from sprintf to subtitle

2 次查看(过去 30 天)
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by