简体   繁体   English

通过 sprintf() 和 fprintf() 的 Matlab ShortEng 数字格式?

[英]Matlab ShortEng number format via sprintf() and fprintf()?

I like using MATLAB's shortEng notation in the interactive Command Window:我喜欢在交互式命令行窗口中使用 MATLAB 的shortEng符号:

>> a = 123e-12;
>> disp(a);

   1.2300e-10       % Scientific notation. Urgh!

>> format shortEng;
>> disp(a);

   123.0000e-012    % Engineering notation! :-D

But I want to use fprintf:但我想使用 fprintf:

>> format shortEng;
>> fprintf('%0.3e', a); 
1.2300e-10          % Scientific. Urgh!

How do I print values with fprintf or sprintf with Engineering formatting using the MATLAB Format Operators ?如何使用 MATLAB Format Operators以 Engineering 格式使用 fprintf 或 sprintf 打印值?

I know I could write my own function to format the values into strings, but I'm looking for something already built into MATLAB.我知道我可以编写自己的函数来将值格式化为字符串,但我正在寻找已经内置到 MATLAB 中的东西。

NOTE: "Engineering" notation differs from "Scientific" in that the exponent is always a multiple of 3.注意:“工程”符号与“科学”符号的不同之处在于指数始终是 3 的倍数。

>> fprintf('%0.3e', a);    % This is Scientific notation.
1.230000e-10

There is no way to use directly fprintf format specifier for the format you require.对于您需要的格式,无法直接使用fprintf格式说明符。 A way around is to use the output of disp as a string to be printed.一种解决方法是使用disp的输出作为要打印的字符串。 But disp doesn't return a string, it writes directly to the standard output.但是disp不返回字符串,它直接写入标准输出。 So, how to do this?那么,如何做到这一点?

Here's where evalc (eval with capture of output) comes to the rescue:这就是evalc (带有捕获输出的 eval)的用武之地:

%// Create helper function
sdisp = @(x) strtrim(evalc(sprintf('disp(%g)', x)));

%// Test helper function
format ShortEng;
a = 123e-12;
fprintf(1, 'Test: %s', sdisp(a));

This is a workaround, of course, and can backfire in multiple ways because of the untested inputs of the helper functions.当然,这是一种解决方法,并且由于辅助函数的输入未经测试,可能会以多种方式适得其反。 But it illustrates a point, and is one of the rare occasions where the reviled eval function family is actually irreplaceable.但它说明了一点,并且是被诟病的eval函数系列实际上不可替代的少数情况之一。

You can use the following utility:您可以使用以下实用程序:

http://www.people.fas.harvard.edu/~arcrock/lib118/numutil/unpacknum.m http://www.people.fas.harvard.edu/~arcrock/lib118/numutil/unpacknum.m

This will unpack the number also according to a given number N and makes sure that the exponent will be a multiple of N .这也将根据给定的数字N解压缩数字,并确保指数将是N的倍数。 By putting N=3 you have the Engineering Notation.通过将N=3设置为工程符号。

More into detail, unpacknum takes 3 arguments: the number x , the base ( 10 if you want Engineering Notation) and the value N ( 3 if you want Engineering Notation) and it returns the couple ( f , e ) which you can use in fprintf() .更详细地说, unpacknum接受 3 个参数:数字x 、基数(如果您想要工程符号,则为10 )和值N (如果您想要工程符号,则为3 ),它返回您可以使用的一对( fefprintf()

Check the unpacknum help for a quick example.查看unpacknum帮助以获取快速示例。

This function converts a value into a string in engineering notation:此函数将值转换为工程符号的字符串:

function sNum = engn(value)
exp= floor(log10(abs(value)));
if ( (exp < 3) && (exp >=0) ) 
    exp = 0; % Display without exponent
else
    while (mod(exp, 3))
        exp= exp - 1;
    end
end
frac=value/(10^exp); % Adjust fraction to exponent
if (exp == 0)
    sNum = sprintf('%+8.5G', frac);
else
    sNum = sprintf('%+8.5GE%+.2d', frac, exp);
end
end

You can finetune the format to your liking.您可以根据自己的喜好微调格式。 Usage in combination with fprintf is easy enough:与 fprintf 结合使用很容易:

fprintf('%s\t%s\n', engn(543210.123), engn(-0.0000567)) % +543.21E+03  -56.7E-06
fprintf('%s\t%s\n', engn(-321.123), engn(876543210)) % -321.12   +876.54E+06

You can use the following utility posted to the MATLAB file exchange: num2eng您可以使用发布到 MATLAB 文件交换的以下实用程序: num2eng

It offers extensive control over the formatting of the output string and full input checking, so is more flexible and less prone to error than the simpler evalc approach suggested by user2271770.它提供了对输出字符串格式和完整输入检查的广泛控制,因此比 user2271770 建议的更简单的 evalc 方法更灵活且不易出错。

It can also output strings using SI prefixes instead of engineering notation, if you prefer.如果您愿意,它还可以使用 SI 前缀而不是工程符号输出字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM