How can I add Column and Row Header to 2d formatted data in pdf using MATLAB Report Generator?

5 views (last 30 days)
I have a 2D matrix that I wanted to write in pdf. Following from my first question, I have done forming of data. Now, I want to add a Column and Row header to it.
I have tried to make the Column and Row Header; how can I append values and export them in pdf.
clear all
addpath('hex_and_rgb_v1.1.1')
addpath('colorGradient')
format short
z=[
0 0.5 1;
0.5 0.3 0.33;
0.8 0.9 0.10;
];
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('FormattedTable','pdf');
tb = Table(z); %Data Table
headerStyle={ Border('none'),...
ColSep('none'),...
RowSep('none'),...
HAlign('center') };
dataHeader = {'Good', 'Happy','Best'};
myheader = Table(4);
myheader.Style = [myheader.Style headerStyle];
%t = FormalTable(myheader ,dataHeader);
%add(rpt,t) %This add the string but not in Table format
dataRowHeader = {'Great', 'Amazing','Owsum'};
rowheader = TableRow;
rowheader.Style = [rowheader.Style headerStyle];
p = Paragraph(' ');
p.Style = [p.Style headerStyle {HAlign('center')}];
te = TableEntry(p);
append(rowheader, te);
% Conditional formatting for table entries
nRows = tb.NRows;
nCols = tb.NCols;
for iRow = 1:nRows
for iCol = 1:nCols
entry = tb.entry(iRow,iCol);
entryContent = entry.Children.Content;
% Provide as many cases you want based on the entry content value
if entryContent < 0.5
entry.Style = {BackgroundColor('red')};
else
entry.Style = {BackgroundColor('green')};
end
end
end
tb.Style = {Width('100%'),...
Border('none'),...
ColSep('none'),...
RowSep('none') };
tb.Width= '3in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';
%tb.Border = 'single';
add(rpt,tb)
close(rpt)
rptview(rpt)
I tried to append the table with the row header, but it throws an error.
I have seen the code on math work to generate the table. But I didn't make much out of it since I am a bit new to Report Generator.
My final output will be the 2D formatted matrix with both Row and Column Header exported in pdf.

Answers (1)

Aditya
Aditya on 21 Nov 2024 at 5:41
I went through the code and it seems that there are a few issues that may prevent it from functioning as expected. Here are some observations and solutions:
Issues:
  • Table Headers: You have defined 'dataHeader' and 'dataRowHeader' but have not properly added them to your table. The 'Table' object 'tb' is created directly from the matrix 'z' without headers.
  • Header Creation: You need to create table headers separately and add them to the table. This involves creating a 'TableRow' for headers and appending it to the table.
Here's is the modified code which works as expected:
clear all
addpath('hex_and_rgb_v1.1.1')
addpath('colorGradient')
format short
z = [
0 0.5 1;
0.5 0.3 0.33;
0.8 0.9 0.10;
];
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('FormattedTable','pdf');
% Create a formal table with headers
dataHeader = {'Good', 'Happy', 'Best'};
headerRow = TableRow();
for i = 1:length(dataHeader)
entry = TableEntry(dataHeader{i});
headerRow.append(entry);
end
tb = Table();
append(tb, headerRow);
% Add data rows with conditional formatting
for iRow = 1:size(z, 1)
row = TableRow();
for iCol = 1:size(z, 2)
entry = TableEntry(num2str(z(iRow, iCol)));
if z(iRow, iCol) < 0.5
entry.Style = {BackgroundColor('red')};
else
entry.Style = {BackgroundColor('green')};
end
append(row, entry);
end
append(tb, row);
end
tb.Style = {Width('100%'), Border('none'), ColSep('none'), RowSep('none')};
tb.Width = '3in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';
add(rpt, tb);
close(rpt);
rptview(rpt);

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!