How to format scientific notation to use "... x 10^..." instead of "...e..."
조회 수: 329 (최근 30일)
이전 댓글 표시
I'm trying to title graphs and legends. I'm trying to get the titles and such to look nice for publication. I'm having issues with the display of scientific notation getting it to use "x 10^" instead of "e".
For example:
title(sprintf('Surface Ions per mm^2: %0.2e', SurfaceIons)) returns 'Surface Ions per mm^2: 2.35e5' in the title. I would like this to read 'Surface Ions per mm^2: 2.35 x 10^5'.
I can't find anything in the documentation on getting scietific notation to display nicely inside graph titles and such. Even using the Latex interpreter for title doesn't seem to work as sprint f puts the e notation into the string that the latex then interprets as the letter e.
Thank you.
댓글 수: 0
채택된 답변
Mike
2021년 3월 30일
Hi Christian,
Try this:
title(sprintf('Surface Ions per nm^2: %0.2f x 10^%i', 10^mod(log10(surfaceIons),1),floor(log10(surfaceIons))))
댓글 수: 1
추가 답변 (1개)
Walter Roberson
2021년 3월 30일
title() and sprintf() do not have any direct support for outputing a number in that format. However, you can use
SurfaceIons = 2.35e5;
S = sprintf('Surface Ions per nm^2: %0.2e', SurfaceIons);
S = regexprep(S, 'e\+?(-?\d+)', 'x 10^{$1}')
title(S)
This version of the code deliberately removes + from exponent, leaving - if present; however this version of the code does not remove leading 0 from the exponent -- which is possible, but I am under a time constraint at the moment.
댓글 수: 2
Walter Roberson
2021년 3월 30일
SurfaceIons = 2.35e5;
S = sprintf('Surface Ions per nm^2: %0.2e %0.2e %0.2e', SurfaceIons, 1/SurfaceIons, pi);
S = regexprep(S, {'e[+-]0+\>', 'e\+?(-?)0*(\d+)'}, {'', '{\\times}10^{$1$2}'})
title(S)
This illustrates a deliberate design choice that if the exponent is 0, that the x 10^0 is not output.
It is also designed to not put leading 0s in the exponent, and to not put in the
It uses the multiplication symbol instead of x.
And it does all the numbers in the same string (so you could easily create a function that does the replacement)
It has the theoretical flaw that it would accept numbers of the form 5e+-7 with both the + and - exponent. That was the easiest way to handle getting rid of + in exponent but leaving - in exponent.
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!