Create custom table in report generator

13 views (last 30 days)
Guglielmo
Guglielmo on 30 Jul 2024
Answered: Abhas on 30 Jul 2024
I need to create a customized table with 3 columns and a variable number of rows. The rows must contain the data that is in an array, which I populate using a For loop. The array has 3 columns but a variable number of rows depending on the subsystem. Is there a field in Simulink Report Generator that allows me to create a table with variable rows and to insert a rule (via code) for each column?
Is it possible to use a script to create this table? If so, what is the correct format?

Answers (1)

Abhas
Abhas on 30 Jul 2024
Hi Gugliemlo,
To create a custom table with 3 columns and a variable number of rows, you need to use the "mlreportgen.dom.Table" class correctly. The following MATLAB code demonstrates an example way to achieve this with sample data:
% Sample data array with variable number of rows and 3 columns
data = [
1, 2, 3;
4, 5, 6;
7, 8, 9
];
% Create a report
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('MyReport', 'pdf');
% Add a title to the report
title = Paragraph('Custom Table Report');
title.Bold = true;
title.FontSize = '14pt';
add(rpt, title);
% Create a table
numRows = size(data, 1);
numCols = size(data, 2);
table = Table();
% Add header row
headerRow = TableRow();
headerContent = {'Column 1', 'Column 2', 'Column 3'};
for col = 1:numCols
entry = TableEntry(headerContent{col});
entry.Style = {Bold(true)};
append(headerRow, entry);
end
append(table, headerRow);
% Populate the table with data
for row = 1:numRows
tableRow = TableRow();
for col = 1:numCols
entry = TableEntry(num2str(data(row, col)));
append(tableRow, entry);
end
append(table, tableRow);
end
numNewRows = 2;
numCols = size(initialData, 2);
for row = 1:numNewRows
tableRow = TableRow();
for col = 1:numCols
randomValue = randi(100); % Random integer between 1 and 100
entry = TableEntry(num2str(randomValue));
append(tableRow, entry);
end
append(table, tableRow);
end
% Add table to report
add(rpt, table);
% Close and view the report
close(rpt);
rptview(rpt);
OUTPUT:
You may refer to the following MathWorks documentation links to have a better understanding on creating tables in reports:
  1. https://www.mathworks.com/help/rptgen/ug/mlreportgen.dom.table-class.html
  2. https://www.mathworks.com/help/rptgen/ug/format-tables.html

Categories

Find more on Reporting and Database Access in Help Center and File Exchange

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!