Report Generator, independently formatting Formal Table columns and changing a header
7 次查看(过去 30 天)
显示 更早的评论
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/721484/image.png)
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:
- Change header content in the Formal Table FT ('X' should be 'Variable X')
- Left align the first column
- Apply Number '%.1f' to the second column, so each cell in the second row will be 1.0 instead of 1.000
- 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!
0 个评论
采纳的回答
Kevin Holly
2021-8-25
编辑:Kevin Holly
2021-8-25
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'})
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 个评论
Kevin Holly
2021-8-26
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!