Formating String Parameter Left of Decimal

조회 수: 1 (최근 30일)
Matthew
Matthew 2017년 9월 25일
편집: Stephen23 2017년 9월 25일
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
Stephen23
Stephen23 2017년 9월 25일
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 2017년 9월 25일
편집: Stephen23 2017년 9월 25일
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

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

채택된 답변

Star Strider
Star Strider 2017년 9월 25일
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
Matthew
Matthew 2017년 9월 25일
편집: Stephen23 2017년 9월 25일
Wonderful! This works perfectly!
Star Strider
Star Strider 2017년 9월 25일
@Matthew —
As always, my pleasure!

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

추가 답변 (1개)

John D'Errico
John D'Errico 2017년 9월 25일
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
Matthew
Matthew 2017년 9월 25일
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 2017년 9월 25일
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.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by