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

2 次查看(过去 30 天)
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)

采纳的回答

Rahul Singhal
Rahul Singhal 2021-2-18
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by