MATLAB Answers

Add header to the splited tables and omit the "" signs

3 views (last 30 days)
Ely Raz
Ely Raz on 21 May 2020
Answered: Rahul Singhal on 15 Jun 2020
Based on this question I wrote a code that generate a PDF report.
Two questions: 1) How can I omit the "" that appear on the each table data (convert it back to number), 2) How can I copy the header to the splited tables?
Thanks.
Code:
import mlreportgen.report.*
import mlreportgen.dom.*
import mlreportgen.utils.*
filename = 'E://wine.csv'; %source: https://gist.githubusercontent.com/tijptjik/9408623/raw/b237fa5848349a14a14e5d4107dc7897c21951f5/wine.csv
T = readtable(filename,'Delimiter',',','PreserveVariableNames',true);
% set desired precision in terms of the number of decimal places
n_decimal = 2;
% create a new table
new_T = varfun(@(x) num2str(x, ['%' sprintf('.%df', n_decimal)]), T);
% preserve the variable names and the row names in the original table
new_T.Properties.VariableNames = T.Properties.VariableNames;
new_T.Properties.RowNames = T.Properties.RowNames;
% display the original table
fprintf('Original table:\n')
T;
% display the new table, which has the desired decimal places
fprintf('New table with %d decimal places:\n', n_decimal);
new_T;
tb = Table(new_T);
rpt = Report('zTable','pdf');
chapter = Chapter("Title","Slice it");
% Set the colors for alternating rows
for i = 1:height(new_T)
r = tb.row(i);
if mod(i,2)==0
r.Style = {BackgroundColor('lightsteelblue')};
else
r.Style = {BackgroundColor('white')};
end
end
tb.Style={RowHeight('0.3in'),RowSep('solid'),ColSep('solid')};
%tb.Width= '2in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';
tb.Border = 'single';
rpt.Layout.Landscape = true;
% Set slice
slicer = mlreportgen.utils.TableSlicer("Table",tb,"MaxCols",10);
slices = slicer.slice();
for slice = slices
str = sprintf("From column %d to column %d",slice.StartCol,slice.EndCol);
para = Paragraph(str);
para.Bold = true;
para.Style = [para.Style,{KeepWithNext(true), OuterMargin("0pt","0pt","5pt","0pt")}];
add(chapter,para);
add(chapter,slice.Table);
end
add(rpt,chapter)
close(rpt)
rptview(rpt)

  0 Comments

Sign in to comment.

Answers (1)

Rahul Singhal
Rahul Singhal on 15 Jun 2020
1) I see that you are converting table numeric data into string using below line of code:
new_T = varfun(@(x) num2str(x, ['%' sprintf('.%df', n_decimal)]), T);
String data in MATLAB table is reported with " ". You can see the difference by reporting on actual table T where the table data is numeric and not string.
2) To repeat headers in splitted tables, I suggest to use DOM MATLABTable or FormalTable class instead of Table class.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!