简体   繁体   English

在Matlab中将科学计数形式的数字转换为字符串

[英]Convert number in scientific notation to string in Matlab

I would like to convert a scientific number to a string in Matlab but using a rather specific format. 我想在Matlab中将科学数字转换为字符串,但使用一种相当特定的格式。 I started with the following: 我从以下内容开始:

>> num2str(1E4, '%e')

ans =

    '1.000000e+04'

Then played around with the formatstring to get rid of the digits after the decimal point in the first part 然后使用formatstring来消除第一部分中小数点后的数字

>> num2str(1E4, '%.0e')

ans =

    '1e+04'

The thing is I want it exactly how I am expressing it in numbers, namely I want a string like this '1E4' . 问题是我想要它确切地用数字来表示,即我想要一个像这样的字符串'1E4' I could use strrep to get rid of that plus sign but I refuse to use it to get rid of the leading 0 on the +04 part since I have other instances of the variable which have things like +10 . 我可以使用strrep摆脱该加号,但我拒绝使用它摆脱+04部分的前导0,因为我还有其他变量实例,例如+10 It it feasible to reproduce the number as a string without resorting to some big complicated algorithm? 将数字重生成为字符串而无需借助一些大的复杂算法是否可行? Preferably using the formatstring? 最好使用formatstring?

Solution

According to num2str documentation, you need to use a format parameter of and precision parameter as follows: 根据num2str文档,您需要使用格式参数和精度参数,如下所示:

num2str(1E4,'%.E')

Result 结果

ans = 1E+04

Read about sprintf . 了解有关sprintf的信息 LEt A be your number, to achieve what you want, you can use: LEA A是您的电话号码,要实现所需的目标,可以使用:

sprintf('%1.0e',A)

Here is a way to convert integers to scientific notation: 这是将整数转换为科学计数法的方法:

function out= scientific(num)
    E = 0;
    if mod(num,10) == 0
        [f n]=factor(num);
        E=min(n(ismember(f,[2 5])));
    end
    out = sprintf('%dE%d',num/10^E,E);
end

>> scientific(134)
ans = 134E0
>> scientific(134000)
ans = 134E3

Another solution that accepts input as vector: 接受输入作为向量的另一种解决方案:

function out= scientific2(num)
    E = sum(cumsum(num2str(num(:))-48,2,'reverse')==0,2);
    out = num2str([num(:)./10.^E,E],'%dE%d\n');
end

You could use a combination of sprintf and regexprep . 您可以结合使用sprintfregexprep

my_format = @(x)regexprep(sprintf('%.E',x),'E\+0*','E');

Examples: 例子:

>> my_format(1E4)

ans =

1E4

>> my_format(2E12)

ans =

2E12

This is not ideal for all cases: 这并非对所有情况都理想:

>> my_format(5) % Expect 5E0

ans =

5E

>> my_format(1E-4) % Expect 1E-4

ans =

1E-04

We can fix the first case with a token : 我们可以用令牌解决第一种情况:

f2 = @(x)regexprep(sprintf('%.E',x),'E\+0*(\d)','E$1');

>> {f2(1E4), f2(1E20), f2(5)}

ans = 

    '1E4'    '1E20'    '5E0'

And we can fix the second case with tokens and a ? 我们可以用令牌和?来解决第二种情况? quantifier : 量词

>> f3 = @(x)regexprep(sprintf('%.E',x),'E\+?(-?)0*(\d)','E$1$2');
>> {f3(1E4), f3(1E20), f3(5),f3(1E-1),f3(2E-12)}

ans = 

    '1E4'    '1E20'    '5E0'    '1E-1'    '2E-12'

To explain, sprintf('%.E',x) formats x in scientific notation with E , eg 1E+04 , then it finds 为了解释, sprintf('%.E',x)格式x科学计数法与E ,例如1E+04 ,然后找到

'E\+?(-?)0*(\d)'

 E                 The literal E
  \+?(-?)          Either a + or a -; if - then save to group $1
         0*        As many 0s as it can match, subject to...
           (\d)    At least one digit, saves digit to group $2

Finally, the matched text is replaced with E$1$2 , that is the literal E , then group $1 (a minus sign if found E- , nothing if found E+ ) and the group $2 (a single digit). 最后,将匹配的文本替换为E$1$2 ,即文字E ,然后是组$1 (如果找到E-则为减号,如果找到E+E+ )和$2组(一位数)。

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

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