Formatting Subscript, and Superscript in Text

Need some help adding subscripts, and superscripts to text descriptions.
title("D_{0} D^{0}")
title(sprintf('D_{0} D^{0}'))
title(sprintf('D_{0} D^{0}')) applies the subscript, and superscript 0 to the title, however if I use sprintf without the title function the subscript, and superscript is not applied.
sprintf('D_{0} D^{0}')
ans = 'D_{0} D^{0}'

回答 (1 件)

Star Strider
Star Strider 2021 年 10 月 10 日

1 投票

Note that the sprintf call is not required for this, unless it is necessary to also print variables —
text(0.4, 0.4, sprintf('D_{0} D^{0}'))
text(0.6, 0.6, sprintf('$D_{0} D^{0}$'), 'Interpreter','latex')
text(0.5, 0.5, 'D_{0} D^{0}')
text(0.7, 0.7, '$D_{0} D^{0}$', 'Interpreter','latex')
Experiment to get different results.
.

4 件のコメント

Willingo
Willingo 2022 年 3 月 25 日
I was working to vectorize this, and while not as elegant as possible, perhaps this code will be of help.
% Vectorization of above process (with some implicit expansion)
exps = "{" + string(1:10)' + "}"
myStrArray = ["10" + "^" + exps]
x = 1:10;
y = 1:10;
scatter(x,y);
text(x,y,myStrArray)
I wish I could run it, but I'm new to this, and it seems that neither Code--> "Insert a line of code" or Insert-->Insert a code example let "Run" work. I just get "Error: Something went wrong. Please try again". The help site is also no help.https://www.mathworks.com/matlabcentral/help/rtc
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2022 年 5 月 19 日
移動済み: KSSV 2024 年 6 月 12 日
Hello Star Strider,
useful tip, as usual.
Thanks, and best!
Captain Karnage
Captain Karnage 2022 年 9 月 22 日
The answer is a useful tip, but the OP's question was not answered. I have the same question. Is there a way for MATLAB to show a superscript or subscript outside of labels on a figure? Like, in the command/output window?
Star Strider
Star Strider 2022 年 9 月 22 日
@Captain Karnage — The Live Editor () might be an option (I rarely use it) however the Command Window is restricted to the ASCII character set. So something like -40°C or e=mc² would appear as it does here, however what isn’t provided in a particular ASCII font wouldn’t be available.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 10 月 10 日

コメント済み:

2024 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by