Precision lost when change numbers to string
Ältere Kommentare anzeigen
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);
fclose(fid);
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 14 Dez. 2017
2 Stimmen
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.
3 Kommentare
Philip Borghesani
am 14 Dez. 2017
Walter,
>>help string.plus
...
Either STR1 or STR2 can be a character array, a cell array, a numeric,
or logical array.
Steven Lord
am 14 Dez. 2017
"If one input is a string array, then the other input can be a numeric, logical, character, string, or cell array."
But I can understand why Walter was unable to find this information -- it's not as visible as it could be. I have created an enhancement request for the documentation staff.
Walter Roberson
am 14 Dez. 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 string.plus 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 ??
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!