Transfer character string from sprintf to subtitle
2 views (last 30 days)
Show older comments
Dennis Premoli
on 23 May 2021
Commented: Star Strider
on 23 May 2021
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.
0 Comments
Accepted Answer
Star Strider
on 23 May 2021
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})
txt = alloy_comps{1,1};
figure
plot((1:10), rand (1,10), 'gp', 'MarkerFaceColor','g')
title('Combined EDX Plots for 151a')
subtitle(txt)
.
2 Comments
Star Strider
on 23 May 2021
AAs always, my pleasure!
Please don’t be ashamed — just be willing to experiment!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!