Engineering Notation Printed Into Files

It seems logical to me that it would be easy to print values out into a file using engineering notation for the exponents, but apparently I'm horribly mistaken. Does anyone know how to do this? I have been googling and looking at help files for 2 days now, and still can't figure it out. I'm thinking it has something to do with the output format on fprintf that was clearly designed by a sadist, but what that format is I can't tell by any of the literature on the subject...

4 件のコメント

Jan
Jan 2011 年 2 月 6 日
Please specify the wanted format exactly: Significant number of digits, specific number of fractional digits, number of digits in the exponent, exponent a multiple of 3, leading +, leading zeros, "10^10" instead of "E010", or "e10", or "g10"??
Mike
Mike 2011 年 2 月 6 日
There's an easy way to get the notation required if all i wanted to do way display it on screen using:
format longEng,
However I need to write that out to a file.
the engineering notation I'm referring to is the same as scientific notation except the exponents are in multiples of 3 (eg 3,6,9,12,-3,-15 etc). For my application it would actually be nicer to have them as K,MEG,G,T,M,A respectively, but I fear that is too much to ask and is easier handled in perl. It doesn't matter how many decimal points there are. I'd prefer to keep it around 4 personally...
Stephen23
Stephen23 2014 年 7 月 15 日
編集済み: Stephen23 2014 年 9 月 10 日
If you need the SI prefixes k, M, G, T, etc, then try out my FEX submission prefixed strings. It provides conversions between numerics and SI or binary prefixed strings (eg: 1000 -> '1 k'), with options to control the significant figures and trailing zero handling.
Star Strider
Star Strider 2014 年 7 月 15 日
A few months ago, I submitted a Support Request to add an engineering notation option to the available field descriptors. Maybe in a future release...?

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2011 年 2 月 6 日

1 投票

(Corrected as per Jan's catch of my typo)
format long eng;
fwrite(fid, evalc('disp(YourVariable)'))

7 件のコメント

Jiro Doke
Jiro Doke 2011 年 2 月 7 日
@Jan: I believe so. I will do the same with this one.
Jiro Doke
Jiro Doke 2011 年 2 月 7 日
@Walter: This is good. Unfortunately, when the matrix has more columns that it can be displayed in the Command Window, you'll see "wrapping" of the matrix with the headers "Column 1 through 8", etc, in between.
Walter Roberson
Walter Roberson 2011 年 2 月 7 日
@Jiro: Mike's comment to the original question that starts with "There's an easy way to get the notation required" implies that the header is part of what is required -- that whatever would be displayed on the screen is what has to be written to the file. The headers are part of what would be displayed on the screen so we must conclude they are part of Mike's specification.
Jiro Doke
Jiro Doke 2011 年 2 月 7 日
@Walter: Perhaps. I read his comment as being specifically about the notation. I didn't interpret as the headers being part of his specification. But I think Mike can clarify that.
Walter Roberson
Walter Roberson 2011 年 2 月 8 日
I feel a bit nauseous that this was selected as the answer... ;-)
Jan
Jan 2011 年 2 月 8 日
@Walter: Your solution is accepts arrays, integer types, NaN, Inf. My solution creates the G, M, k specifiers, which contradicts the original question (it is not the engineering notation), but the comment of the OP. So be proud.
Stephen23
Stephen23 2025 年 3 月 19 日
Since R2021a:
txt = formattedDisplayText(YourVariable, 'NumericFormat','longEng')

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2011 年 2 月 6 日

2 投票

function Str = EngineersStyle(x)
Exponent = 3 * floor(log10(x) / 3);
y = x / (10 ^ Exponent);
ExpValue = [9, 6, 3, 0, -3, -6, -9];
ExpName = {'G', 'M', 'k', '', 'm', 'my', 'n'};
ExpIndex = (Exponent == ExpValue);
if any(ExpIndex) % Found in the list:
Str = sprintf('%f%s', y, ExpName{ExpIndex});
else % Fallback: Show the numeric value
% EDITED: Walter refined '%d' to '%+04d':
Str = sprintf('%fe%+04d', y, Exponent);
end
Please adjust the sprintf format expand the list of exponent names accoriding to your needs.

7 件のコメント

Walter Roberson
Walter Roberson 2011 年 2 月 6 日
format long eng
always uses a sign for the exponent and always includes 3 digits of exponent. That requires using %+04d for the exponent format.
Jan
Jan 2011 年 2 月 6 日
@Walter: Good addition, I'll include it. Do you mean %+03d?
Walter Roberson
Walter Roberson 2011 年 2 月 6 日
You need to include the sign itself in the count, so +04d for one sign character and 3 exponent characters.
Jürgen Stein
Jürgen Stein 2016 年 11 月 19 日
Exponent = 3 * floor(log10(x) / 3 +100*eps);
There is a numerical issue with the code: If you try to print 1e9 the function output will show 1000M instead of 1G. This is due to the fact that floor(log10(1e9)/3) will deliver a 2 instead of the expected value of 3 because of the numerical accuracy of the log10 implementation. Try it! The dirty fix will help.
Walter Roberson
Walter Roberson 2016 年 11 月 20 日
R2016b, OS-X, floor(log10(1e9)/3) does give 3.
Jan
Jan 2016 年 11 月 21 日
編集済み: Jan 2016 年 11 月 21 日
R2009b, Win32/64: floor(log10(1e9)/3) replies 3 also. log10 has been instable in R6.5, as far as I remember, but afterwards it has been fixed.
format long g
exponent = -200:+200;
value = 10 .^ exponent;
log10(value)
This replies integer values and the division by 3 works as expected.
@Jürgen: which OS are you working on?
Harry Dymond
Harry Dymond 2019 年 7 月 15 日
編集済み: Harry Dymond 2019 年 7 月 16 日
A long time ago I wrote a function inspired by this post by Jan (thank you, Jan!), and over the years expanded its functionality. More recently I submitted it to the FEX; check it out: num2eng

サインインしてコメントする。

Walter Roberson
Walter Roberson 2011 年 2 月 6 日

1 投票

Let B be a vector of values you want to print. Then,
C = floor(log(B(:))/log(1000));
sprintf('%gE%+04d ', [B(:) ./ 1000.^C, 3.*C].')
This can be modified if you need a fixed number of digits after the decimal place, by using (e.g.) %.3f instead of %g .
If you need a fixed total number of digits (e.g., use more digits after the decimal place if fewer are used before the decimal place), matters get more complicated. You can get close to that easily, but that particular mechanism trims trailing 0's after it has truncated to the desired number of total digits.
Walter Roberson
Walter Roberson 2011 年 2 月 6 日

0 投票

Matlab does not offer any built-in formatting of strings in engineering format.
I have, by the way, seen at least two different "engineering notation"s. What format are you interested in? In particular, sometimes engineering format uses commas as thousands separators and sometimes it does not. (I have no idea what Engineering Format is like in non-English languages.)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

質問済み:

2011 年 2 月 6 日

コメント済み:

2025 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by