Report Generator, independently formatting Formal Table columns and changing a header

Hello! I am new to the DOM and report generator, and I wasn't able to accomplish my reporter relying solely on documentation.
I have created the Formal Table from Matlab Table (and added it to a report later).
import mlreportgen.report.*
import mlreportgen.dom.*
X = zeros(5,1);
Y = ones(5,1);
Z = ["A";"B";"C";"D";"E"];
T = table(X,Y,Z)
FT = FormalTable(T); % Formal table from a regular table
styleT = {NumberFormat("%1.3f"),...
Border('inset','black','2px'), ...
ColSep('single','black','1px'), ...
RowSep('single','black','1px')};
FT.Style = [FT.Style styleT]; % Applying common style
rpt = Report('A');
add(rpt,FT);
close(rpt);
rptview(rpt);
Now I want:
  1. Change header content in the Formal Table FT ('X' should be 'Variable X')
  2. Left align the first column
  3. Apply Number '%.1f' to the second column, so each cell in the second row will be 1.0 instead of 1.000
  4. Represent content of the 3rd column in the Formal Table without quotes
I am sure all these thing are possible. Please help me.
Thank you!

 Accepted Answer

X = zeros(5,1);import mlreportgen.report.*
import mlreportgen.dom.*
X = zeros(5,1);
Y = ones(5,1);
Z = ["A";"B";"C";"D";"E"];
T = table(X,Y,categorical(Z),'VariableNames',{'Variable X', 'Variable Y','Variable Z'})
T = 5×3 table
Variable X Variable Y Variable Z __________ __________ __________ 0 1 A 0 1 B 0 1 C 0 1 D 0 1 E
FT = FormalTable(T); % Formal table from a regular table
styleT = {NumberFormat("%1.3f"),...
Border('inset','black','2px'), ...
ColSep('single','black','1px'), ...
RowSep('single','black','1px')};
FT.Style = [FT.Style styleT]; % Applying common style
FT.Style{4}.Value='%.1f';
%Alignment
groups(1) = TableColSpecGroup();
specs(1) = TableColSpec();
specs(1).Style = { HAlign('left') };
specs(2) = TableColSpec();
specs(2).Style = { HAlign('center') };
specs(3) = TableColSpec();
specs(3).Style = { HAlign('center') };
groups(1).ColSpecs = specs;
FT.ColSpecGroups = groups;
%Generate Report
rpt = Report('A');
add(rpt,FT);
close(rpt);
rptview(rpt);

5 Comments

Thanks, Kevin!
  1. Changing the Matlab table's header (not FT) gives a warning (below), so I was wondering if it is possible to change the formal's table header directly. Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
  2. Alignment works perfectly!
  3. Also, the code FT.Style{4}.Value='%.1f' changes all columns format. But I would like to format only the second column (Y). All other columns must remain untouched, cells in column X must be 0.000.
  4. Categorical works fine for me for getting rid of quotes.
import mlreportgen.report.*
import mlreportgen.dom.*
X = cellstr(num2str(zeros(5,1),'%1.3f'));
Y = cellstr(num2str(ones(5,1),'%.1f'));
Z = ["A";"B";"C";"D";"E"];
T = table(categorical(X),categorical(Y),categorical(Z),'VariableNames',{'Variable X', 'Variable Y','Variable Z'})
T = 5×3 table
Variable X Variable Y Variable Z __________ __________ __________ 0.000 1.0 A 0.000 1.0 B 0.000 1.0 C 0.000 1.0 D 0.000 1.0 E
FT = FormalTable(T); % Formal table from a regular table
styleT = {Border('inset','black','2px'), ...
ColSep('single','black','1px'), ...
RowSep('single','black','1px')};
FT.Style = [FT.Style styleT]; % Applying common style
groups(1) = TableColSpecGroup();
specs(1) = TableColSpec();
specs(1).Style = [{HAlign('left')}];
specs(2) = TableColSpec();
specs(2).Style = [{HAlign('center')}];
specs(3) = TableColSpec();
specs(3).Style = { HAlign('center')};
groups(1).ColSpecs = specs;
FT.ColSpecGroups = groups;
rpt = Report('A');
add(rpt,FT);
close(rpt);
rptview(rpt);
Here is how you can edit the header content after using FormalTable. This gets rid of the warning.
import mlreportgen.report.*
import mlreportgen.dom.*
X = cellstr(num2str(zeros(5,1),'%1.3f'));
Y = cellstr(num2str(ones(5,1),'%.1f'));
Z = ["A";"B";"C";"D";"E"];
T = table(categorical(X),categorical(Y),categorical(Z));
FT = FormalTable(T); % Formal table from a regular table
styleT = {Border('inset','black','2px'), ...
ColSep('single','black','1px'), ...
RowSep('single','black','1px')};
FT.Style = [FT.Style styleT]; % Applying common style
FT.Header.Children.Children(1).Children.Content = 'Variable X';
FT.Header.Children.Children(2).Children.Content = 'Variable Y';
FT.Header.Children.Children(3).Children.Content = 'Variable Z';
groups(1) = TableColSpecGroup();
specs(1) = TableColSpec();
specs(1).Style = [{HAlign('left')}];
specs(2) = TableColSpec();
specs(2).Style = [{HAlign('center')}];
specs(3) = TableColSpec();
specs(3).Style = { HAlign('center')};
groups(1).ColSpecs = specs;
FT.ColSpecGroups = groups;
rpt = Report('A');
add(rpt,FT);
close(rpt);
rptview(rpt);
Great, no need to suppress the warning then. Thanks again

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Asked:

on 25 Aug 2021

Commented:

on 26 Aug 2021

Community Treasure Hunt

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

Start Hunting!