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 Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by