Precision lost when change numbers to string
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
H R
le 13 Déc 2017
Commenté : Walter Roberson
le 14 Déc 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);
fclose(fid);
0 commentaires
Réponse acceptée
Philip Borghesani
le 14 Déc 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.
0 commentaires
Plus de réponses (1)
Walter Roberson
le 14 Déc 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.
3 commentaires
Steven Lord
le 14 Déc 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
le 14 Déc 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 ??
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!