MATLAB Answers

Precision lost when change numbers to string

27 views (last 30 days)
H H on 13 Dec 2017
Commented: Walter Roberson on 14 Dec 2017
Hi I am trying to use the following to generate a string. A is is 1D array containing 4 real numbers that have large decimal precisions
A=[0.000002101, 0.000000225567 ,1.20004136789, 66.000052353]
ST=strjoin( string('Hi ') + A.', newline )
However string 'ST' trims or rounds off my numbers. How can I create the string ST with exact numbers in A?
fid = fopen('ny.txt','wt');
fprintf(fid, '%s', ST);

Accepted Answer

Philip Borghesani
Philip Borghesani on 14 Dec 2017
Similar to how you were doing it:
ST=join("Hi " + num2str(A',"%1.12e"), newline )
ST =
"Hi 2.101000000000e-06
Hi 2.255670000000e-07
Hi 1.200041367890e+00
Hi 6.600005235300e+01"
Or using sprintf like Walter suggested:
>> sprintf('Hi %1.12e\n',A)
ans =
'Hi 2.101000000000e-06
Hi 2.255670000000e-07
Hi 1.200041367890e+00
Hi 6.600005235300e+01
Note that the first method returns a string without a trailing newline and sprintf returns a character vector with one.

More Answers (1)

Walter Roberson
Walter Roberson on 14 Dec 2017
I am not able to find any documentation establishing that using + between string and number is intended to work at all -- though I know that it is intended to work.
You will need to use sprintf() to do fine-grained formatting of numbers into strings.
Walter Roberson
Walter Roberson on 14 Dec 2017
The information about + for strings cannot be found from the documentation of the string data type; it is not indexed in the operations.
The help and the plus documentation do mention that those kinds of mixes are permitted, but say nothing about what the output will be in such a case. I explored last night and found that there is no single format specification that is used, and that the result depends upon the mix of numbers:
>> "Hi " + [1.234567890123456]
ans =
"Hi 1.2346"
so 4 digits after the decimal for this entry?
>> "Hi " + [1.234567890123456;A.']
ans =
5×1 string array
"Hi 1.23457"
"Hi 2.101e-06"
"Hi 2.25567e-07"
"Hi 1.20004"
"Hi 66.0001"
so 5 digits after the decimal for that same entry?
>> "Hi " + [1.234567890123456; 123456789012345 ./ 10.^(1:30).']
ans =
31×1 string array
"Hi 1.234567890123456"
"Hi 12345678901234.5"
"Hi 1234567890123.45"
"Hi 123456789012.345"
"Hi 12345678901.2345"
"Hi 1234567890.12345"
"Hi 123456789.012345"
"Hi 12345678.9012345"
"Hi 1234567.89012345"
"Hi 123456.789012345"
"Hi 12345.6789012345"
"Hi 1234.56789012345"
"Hi 123.456789012345"
"Hi 12.3456789012345"
"Hi 1.23456789012345"
"Hi 0.123456789012345"
"Hi 0.0123456789012345"
"Hi 0.00123456789012345"
"Hi 0.000123456789012345"
"Hi 1.23456789012345e-05"
"Hi 1.23456789012345e-06"
"Hi 1.23456789012345e-07"
"Hi 1.23456789012345e-08"
"Hi 1.23456789012345e-09"
"Hi 1.23456789012345e-10"
"Hi 1.23456789012345e-11"
"Hi 1.23456789012345e-12"
"Hi 1.23456789012345e-13"
"Hi 1.23456789012345e-14"
"Hi 1.23456789012345e-15"
"Hi 1.23456789012345e-16"
so 14 digits after the decimal for the same 1.* value ??

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!