# How to format scientific notation to use "... x 10^..." instead of "...e..."

485 vues (au cours des 30 derniers jours)
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### 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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Christian Chamberlayne le 30 Mar 2021
Modifié(e) : Christian Chamberlayne le 30 Mar 2021
Thank you!
Thats quite clever. I didn't think about turning it into 2 seperate variables entered seperately into the string.
Edit: Just in case someone else uses this. Put {} around the %i to make sure that integers with muliple char like -3 or 13 superscripts properly. I found out when using this.
like so:
title(sprintf('Surface Ions per nm^2: %0.2f x 10^{%i}', 10^mod(log10(surfaceIons),1),floor(log10(surfaceIons))))

Connectez-vous pour commenter.

### 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}') S = 'Surface Ions per nm^2: 2.35x 10^{05}' 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 commentairesAfficher AucuneMasquer Aucune Christian Chamberlayne le 30 Mar 2021 Thank you! 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}'})
S = 'Surface Ions per nm^2: 2.35{\times}10^{5} 4.26{\times}10^{-6} 3.14'
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.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Title 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!

Translated by