Search code examples
matlabscientific-notation

matlab scientific notation issue


I have 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.


Solution

  • 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.