MATLAB Answers

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

2 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?
import mlreportgen.dom.*
import mlreportgen.utils.*
filename = 'E://wine.csv'; %source:
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')
% display the new table, which has the desired decimal places
fprintf('New table with %d decimal places:\n', n_decimal);
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')};
r.Style = {BackgroundColor('white')};
%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")}];

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.

Community Treasure Hunt

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

Start Hunting!