How to format scientific notation to use "... x 10^..." instead of "...e..."
346 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Christian Chamberlayne
le 30 Mar 2021
Commenté : Walter Roberson
le 30 Mar 2021
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 commentaires
Réponse acceptée
Mike
le 30 Mar 2021
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 commentaire
Plus de réponses (1)
Walter Roberson
le 30 Mar 2021
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 commentaires
Walter Roberson
le 30 Mar 2021
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.
Voir également
Catégories
En savoir plus sur LaTeX dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!