Transfer character string from sprintf to subtitle

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.

 Respuesta aceptada

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 comentarios

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!!
AAs always, my pleasure!
Please don’t be ashamed — just be willing to experiment!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Report Generator en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Mayo de 2021

Comentada:

el 23 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by