Format numerical data in Table within programatic report generation

5 vues (au cours des 30 derniers jours)
Justin Solomon
Justin Solomon le 19 Juin 2017
Commenté : Jan Zelenka le 17 Déc 2021
Hello all, I am generating a report programmatically (pdf) and I'm trying to figure out a simple way to control how the numerical data is formatted when its output into the report. Basically I have a Matlab table, which contains some columns of text and some columns of numerical data. I can easily turn that into a cell array (table2cell) and then use that cell array to construct a table in the document. For example...
d; %Handle to document object
tab; %This is my matlab table of data
%Turn the table into a cell array
c = table2cell(tab);
%Construct the table
t = Table(c)
%append table to the doc
append(d,t)
% close the doc (which saves it to a file)
close(d)
The problem with this method is that numerical data in the table gets printed with full precision and thus has way too many digits (e.g., pi would show up as 3.1415926536 but I'd want something more like 3.14). Is there a simple way to specify that certain columns should be formatted in a specific way, using formatting commands such as those in sprintf()? I know I could loop over each table entry and convert each entry to a string using sprintf(), but I was hoping there was a more elegant/simple way apply numerical formatting options to the whole table at once. I'm relatively novice to programatic report generation so any help is appreciated.
Thanks! Justin
  2 commentaires
Jeenal Rambhia
Jeenal Rambhia le 27 Avr 2019
I am facing the same issue. The value in xcel sheet is 0.6 which after reading in report comes to 0.59999999999999 which is not needed. any solution to this is very helpful.
Thanks
Jan Zelenka
Jan Zelenka le 17 Déc 2021
See https://se.mathworks.com/help/rptgen/ug/format_numbers_in_a_table.html

Connectez-vous pour commenter.

Réponses (4)

Walter Roberson
Walter Roberson le 19 Juin 2017
No, there is not. However, you might want to experiment with using appropriate round() commands on the data before putting it into the document.

Aleksander Karolczuk
Aleksander Karolczuk le 7 Juil 2019
you can try to convert numeric data to string before table creation. Such as,
a=string(a);
T=table(a);
tbl = mlreportgen.dom.MATLABTable(T);

Carol Smyth
Carol Smyth le 2 Sep 2019
I'm having the same issue. Did you find a solution for this?

Xudong Zhou
Xudong Zhou le 5 Déc 2019
i had the same issue, the number had 0.00000000000000001 added or minused in the table, i used string(number) to fix this problem. hope this can help you.

Community Treasure Hunt

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

Start Hunting!

Translated by