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

20 ビュー (過去 30 日間)
Mark Anderson
Mark Anderson 2023 年 3 月 27 日
編集済み: Walter Roberson 2023 年 7 月 22 日
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 件のコメント
Mark Anderson
Mark Anderson 2023 年 3 月 27 日
Thanks!
Walter Roberson
Walter Roberson 2023 年 3 月 27 日
writetable() for xml completely ignores the format that is in effect.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 3 月 27 日
編集済み: Walter Roberson 2023 年 7 月 22 日
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 件のコメント
Sanchit
Sanchit 2023 年 7 月 22 日
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 2023 年 7 月 22 日
編集済み: Walter Roberson 2023 年 7 月 22 日
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)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by