MATLAB: Precision lost when change numbers to string

MATLABstringstrjoin

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);

Best Answer

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.