开发者

matlab scientific notation issue

开发者 https://www.devze.com 2023-03-11 22:24 出处:网络
I ha开发者_StackOverflow中文版ve an issue where an 8-digit string such as \'313397E9\' is being passed into excel, then read as the number \'313397000000000\', or \'3.133970e+014\'.

I ha开发者_StackOverflow中文版ve an issue where an 8-digit string such as '313397E9' is being passed into excel, then read as the number '313397000000000', or '3.133970e+014'.

This is then being read by Matlab and recognized as the number, not the string. What would be the easiest way to convert it back to the 8-digit string?

Thanks in advance for all your help.


Regular expressions to the rescue! You can use REGEXPREP to convert the trailing zeros into Ex, where x is the number of zeros you just replaced.

%# convert the number to a string
nn = num2str(3.133970e+014)

nn =
313397000000000

%# replace zeros using regexprep
regexprep(nn,'([0]*)','E${num2str(length($1))}')
ans =
313397E9

This also works if nn is a cell array of strings, btw, so you can convert your list of numbers in one go.

0

精彩评论

暂无评论...
验证码 换一张
取 消