MATLAB Answers

How can I export a Matlab 2D array in Conditional Formatted table in pdf using MATLAB Report Generator?

2 views (last 30 days)
I have some 2D array in Matlab. And I wanted to export this array in pdf in 3 Colour scheme Conditional Formated Table as shown below. How can I do that?
I have searched a code that generate the zebra strip tables. Does there exist such codes to auto do Conditional Formating (Like in Excel)?
I am new to MATLAB Report Generator,How can I achive this task?
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('zebraTable','pdf');
maglen = 8;
mag = magic(maglen);
tb = Table(mag);
% Set the colors for alternating rows
for i = 1:maglen
r = tb.row(i);
if mod(i,2)==0
r.Style = {BackgroundColor('lightsteelblue')};
else
r.Style = {BackgroundColor('white')};
end
end
tb.Style={RowHeight('0.3in'),RowSep('solid'),ColSep('solid')};
tb.Width= '3in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';
tb.Border = 'single';
add(rpt,tb)
close(rpt)
rptview(rpt)

Accepted Answer

Rahul Singhal
Rahul Singhal on 18 Feb 2021
Hi Masood,
In the sample script that you provided, you can just update the for-loop to provide conditional formatting for each table entry based on the content of the entry. Below is an example:
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('zebraTable','pdf');
maglen = 8;
mag = magic(maglen);
tb = Table(mag);
% 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 < 25
entry.Style = {BackgroundColor('red')};
else
entry.Style = {BackgroundColor('green')};
end
end
end
tb.Style={RowHeight('0.3in'),RowSep('solid'),ColSep('solid')};
tb.Width= '3in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';
tb.Border = 'single';
add(rpt,tb)
close(rpt)
rptview(rpt)
Hope this helps!
Thanks,
Rahul

More Answers (0)

Community Treasure Hunt

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

Start Hunting!