How to display a number in a scientific notation in the plot?

The code is below:
n=1000;
text(0.82,0.96, "$N =\mbox{ } $" +num2str(n), 'interpreter', 'latex', 'FontSize', 38) ;
I want that, "N =10^3" be displayed and not "N = 1000".

8 件のコメント

Rik
Rik 2022 年 1 月 14 日
Only the first and last line are relevant.
What did you attempt? And is n guaranteed to be a power of 10?
It is probably easiest to use the sprintf function and edit that output.
Vikash Pandey
Vikash Pandey 2022 年 1 月 14 日
@Rik Thank you. I have edited the code. Yes, N will always be in a power of 10. I want the text to be written at a specific location on the plot, I am not sure that can be achieved using sprintf function. Please help.
Walter Roberson
Walter Roberson 2022 年 1 月 14 日
text(0.82,0.96, sprintf("$N =\\mbox{10^{%d}}$", log10(n)), 'interpreter', 'latex', 'FontSize', 38) ;
Vikash Pandey
Vikash Pandey 2022 年 1 月 14 日
@Walter Roberson Thank you for your efforts, but this is not working out.
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
$N =\mbox{10^{3}}$
Vikash Pandey
Vikash Pandey 2022 年 1 月 14 日
@ All, leave it. I have hardcoded it. That was the quick fix I could do. Thank you anyways.
Rik
Rik 2022 年 1 月 14 日
That is exactly the result of the code that Walter provided:
n=1000;
sprintf("$N =\\mbox{10^{%d}}$", log10(n))
ans = "$N =\mbox{10^{3}}$"
The problem is your \mbox .
n = 1000;
text(0.82,0.96, sprintf("$N ={10}^{%d}$", log10(n)), 'interpreter', 'latex', 'FontSize', 18) ;
The support TeX markup is shown at https://www.mathworks.com/help/matlab/ref/matlab.graphics.primitive.text-properties.html#budt_bq-1_sep_shared-Interpreter . Unfortunately I do not see a list of supported latex markup.
Vikash Pandey
Vikash Pandey 2022 年 1 月 16 日
Thanks Walter.

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

回答 (1 件)

Robert U
Robert U 2022 年 1 月 14 日

0 投票

Hi Vikash Pandey,
have a look at the function num2eng from Mathworks File Exchange. You can change easily the tick label:
fh = figure;
ah = axes(fh);
ph = plot(ah,1000:1000:10000,0:1000:9000)
ah.XTickLabel = num2eng(ah.XTick)
ah.YTickLabel = num2eng(ah.YTick)
Kind regards,
Robert

1 件のコメント

Vikash Pandey
Vikash Pandey 2022 年 1 月 14 日
@Robert U Thanks for your efforts, but it seems you misunderstood the question.

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

カテゴリ

製品

リリース

R2019b

タグ

質問済み:

2022 年 1 月 14 日

コメント済み:

2022 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by