Formating String Parameter Left of Decimal

1 vue (au cours des 30 derniers jours)
Matthew
Matthew le 25 Sep 2017
Modifié(e) : Stephen23 le 25 Sep 2017
In the following code, I want to create the str variable to read: Radius=100E-6 meter
However, I keep getting: Radius=1.00E-4 meter
I can't seem to force it to place more digits to the left of the decimal. How do I revise sprintf to produce the former instead of the latter?
a=100E-6;
str=sprintf('Radius=%3.0E meter,',a);
I know the answer is probably something simple, but I just can't seem to figure it out.
Thanks in advance,
M Ridzon
  2 commentaires
Stephen23
Stephen23 le 25 Sep 2017
The standard C-based MATLAB command sprintf does not produce "engineering" exponents, it always produces exponents with one digit before the decimal point. You might like to read these (but don't use the first answer!):
Stephen23
Stephen23 le 25 Sep 2017
Modifié(e) : Stephen23 le 25 Sep 2017
As an alternative you could download my FEX submission num2sip, which generates SI/metric prefixes:
>> num2sip(1.234e-4)
ans = 123.4 u
Following your example you could do something like this:
>> sprintf('Radius = %smeter',num2sip(1.234e-4,[],true))
ans = Radius = 123.4 micrometer
Note that unlike the accepted answer num2sip correctly returns the requested precision, regardless of the order of the number:
>> num2sip(1.4567e-4)
ans = 145.67 u
>> num2sip(1.4567e-5)
ans = 14.567 u
>> num2sip(1.4567e-6)
ans = 1.4567 u

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 25 Sep 2017
I wrote a little utility function for my own purposes to do this a while ago:
engstr = @(x) [x*10.^(-3*floor(log10(abs(x))/3)) 3*floor(log10(abs(x))/3)]; % Engineering Notation Mantissa & Exponent
x = 1E-4;
Result = sprintf('%3.0fE%-.0f', engstr(x))
Result =
'100E-6'
Configure the sprintf format string to produce the output you want.
  3 commentaires
Matthew
Matthew le 25 Sep 2017
Modifié(e) : Stephen23 le 25 Sep 2017
Wonderful! This works perfectly!
Star Strider
Star Strider le 25 Sep 2017
@Matthew —
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 25 Sep 2017
Personally, 100e-6 seems like a silly and confusing way to write the number 1e-4. And at some point in my educational career, I even spent some time as an engineer. What you are probably looking for is known as engineering notation.
format SHORTENG
a = 1e-4
a =
100.0000e-006
So can you just display the number directly? I don't see a simple way to force sprintf to work, as it does not seem to include the engineering notation form as an option.
  2 commentaires
Matthew
Matthew le 25 Sep 2017
Thanks for the input. I want to use str in a title command being used to generation a post-processing plot. E-6 notation makes sense in this problem since it clearly communicates that the radius is derived from the micrometer scale, but being reported in the meter scale. And thus it is very obvious to the person reading the plot. But it sounds like I'm unable to use this approach since engineering format isn't available. Oh well, I'll come up with plan B.
Thanks, M Ridzon
John D'Errico
John D'Errico le 25 Sep 2017
Personally, I was surprised that sprintf did not offer engineering notation as an option. I suppose you could write a utility that worked like sprintf, but used engineering notation. It would take some amount of effort, that might not be worth the mental energy expended.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings 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