Superscripts and subscripts in fprintf
Mostrar comentarios más antiguos
Is it possible to display text with subscripts using fprintf in R2016a ? I would like to change Nmax.
fprintf('\nMaximum value of N is Nmax = 2\n');
Respuestas (3)
In MATLAB R2016a, "fprintf" does not support displaying text with subscripts directly because it outputs plain text, and subscripts are a feature of formatted text (like LaTeX or rich text). However, there are workarounds to display subscripts depending on the output medium:
- If you need to display subscripts in a figure, you can use text or annotation with TeX or LaTeX formatting, which supports subscripts:
figure;
text(0.1, 0.5, 'Maximum value of N is N_{max} = 2', 'Interpreter', 'tex');
- To write subscripts using Unicode characters in MATLAB or any plain text context, you can refer the following link and use Unicode subscript characters: https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
fprintf('y\x2098')
I hope this helps!
4 comentarios
Left Terry
el 30 de Dic. de 2024
Abhas
el 30 de Dic. de 2024
"Nₘₐₓ" in MATLAB can't be directly written using "fprintf", I just gave the workaround for few subscripts and superscripts that you can use.
It doesn't work.
It does in LiveScript or other rich text environments:
myfprintf('Maximum value of N is N_{max} = 2')
myfprintf('Minimum value of N is N_{min} = 1')
function myfprintf(str)
hF=figure;hAx=gca;
imagesc(nan(10,500),'AlphaData',0); axis image; axis off;
Ht=text(250,5,str,'Interpreter','tex','Horiz','right');
hF.InnerPosition(end)=hAx.OuterPosition(end)*30;
end
Not all letters have unicode subscripts but m, a, and x do, so you're in luck.
x = 2;
fprintf(1,'Maximum value of N is N%c%c%c=%.0f\n',[8344 8336 8339 x]);
More info
- webnots page that lists unicodes for super- and subscript letters
- subscript generator - to get the numeric unicode values, copy the results in matlab and run double('<results>')
5 comentarios
Left Terry
el 30 de Dic. de 2024
It looks like it worked to me. I can see it right above. The max is smaller, just as a subscript should look. Why do you say it didn't work?
OK, let me try it myself:
Nmax = 2;
fprintf(1,'Maximum value of N is N%c%c%c=%.0f\n',[8344, 8336, 8339, Nmax]);
Looks good to me. Can you post a screenshot of it not working for you? Note if you're fprinting to a plain text file, it may not appear right (I didn't try it), but for the command line, it looks fine.
"It looks like it worked to me. I can see it right above. The max is smaller, just as a subscript should look. Why do you say it didn't work?"|
It does not work in R2016a, which the OP explicitly stated that they are using:

If I recall correctly, it was only around R2018 or R2019 that MATLAB's default MONOSPACED font started to display Unicode characters (more) reliably.
Walter Roberson
el 31 de Dic. de 2024
Ah, that would be a matter of setting the command window font to one that supports unicode.
Left Terry
el 31 de Dic. de 2024
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


