Engineering Notation Printed Into Files
조회 수: 30 (최근 30일)
이전 댓글 표시
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
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
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
2011년 2월 6일
(Corrected as per Jan's catch of my typo)
format long eng;
fwrite(fid, evalc('disp(YourVariable)'))
댓글 수: 7
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
2025년 3월 19일
Since R2021a:
txt = formattedDisplayText(YourVariable, 'NumericFormat','longEng')
추가 답변 (3개)
Jan
2011년 2월 6일
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
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
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
2011년 2월 6일
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.
댓글 수: 0
Walter Roberson
2011년 2월 6일
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.)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!