How to make exponents superscripted on plots instead of with e notation?

조회 수: 5 (최근 30일)
I am displaying a variable in the title of a plot but I would like it to show with the exponent part superscripted instead of in the e notation. For example:
n0=10^25
plot(...)
title('n0')
This displays n0 as 1.0e+25 in the title instead of the 25 superscripted. Is there a way to do this in MATLAB without having to manually type a new string into the title every time?
I don't want to have to do this:
title('10^{25}')
Thanks for any help.

채택된 답변

zero1342
zero1342 2015년 7월 1일
ANSWER:
exponent = floor(log10(n0));
title(sprintf('n_{0} = %.2f x 10^{%d}',n0/10^exponent,exponent))

추가 답변 (1개)

Muthu Annamalai
Muthu Annamalai 2015년 7월 1일
Whereas some options may exist, a simple work around does the trick following your hint;
var2exp = @( varname, var ) [regexprep(sprintf('%s=%g',varname,var),'[e|E]','^{'),'}']
title(var2exp('n0',n0))
this is admittedly kludgy, but seems to suit your needs.
  댓글 수: 2
zero1342
zero1342 2015년 7월 1일
That's close but it doesn't display the 10 in the title, only a superscripted 25 above a 1.
Instead I've done this:
exponent = floor(log10(n0));
title(sprintf('n_{0} = %.2f x 10^{%d}',n0/10^exponent,exponent))
This code takes the exponent off the variable then reattaches it in the title.
Muthu Annamalai
Muthu Annamalai 2015년 7월 23일
편집: Muthu Annamalai 2015년 7월 23일
I'm not so sure.
Whereas my code will work for -ve numbers, your code may fail, and badly at that with some complex output for log10 of a 0 or -ve number.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by