Writetable() truncates numbers to four decimal places in XML file

21 vues (au cours des 30 derniers jours)
Mark Anderson
Mark Anderson le 27 Mar 2023
I have data in a table that has 8 decimal places, but when I use writetable() to write the table to an XML file, the numbers are truncated to 4 decimal places. How can I write all 8 decimal places to the XML file?
  3 commentaires
Mark Anderson
Mark Anderson le 27 Mar 2023
Thanks!
Walter Roberson
Walter Roberson le 27 Mar 2023
writetable() for xml completely ignores the format that is in effect.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Mar 2023
Modifié(e) : Walter Roberson le 22 Juil 2023
The internal code in matlab.io.xml.internal.write.writeTable uses string() on the numeric data and string() outputs 5 significant digits for values with absoute value less than 1, and at most four digits after the decimal place for elements with absolute value greater than 1
Unfortunately no way is provided in writetable() to change the precision, and no way is provided to alter how string() converts numbers.
This means that your work-around is going to have to be to convert the elements to string yourself, such as
newT = varfun(@(V) compose("%.17g",V), T);
newT.properties.VariableNames = T.Properties.VariableNames;
  3 commentaires
Sanchit
Sanchit le 22 Juil 2023
My data is generated as follows
data = groupsummary(dataTbl,["Time","Time"],["hourofday","day"],"mean",4:12);
How do I write these nine variables(4:12) without truncated in a csv file?
Sanchit
Walter Roberson
Walter Roberson le 22 Juil 2023
Modifié(e) : Walter Roberson le 22 Juil 2023
Could you show me the first few lines of the resulting table data? And confirm which of the variable names are to be written? Is time information to be written?
Try
T2 = convertvars(data, 'double', @(V) compose("%.17g", V));
and then writetable() T2(:,4:12)

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by