如何使用 MATLAB 报告生成器以 pdf 格式导出条件格式 table 的 Matlab 二维数组?

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

我在 Matlab 中有一些二维数组。我想以 3 种配色方案条件格式(Excel 中最常用的函数)Table 以 pdf 格式导出此数组,如下所示。我怎样才能在 MATLAB 中做到这一点?

我已经写了一个脚本来做到这一点。但是我无法调试我做错了什么。

addpath('hex_and_rgb_v1.1.1')
addpath('colorGradient')
format short

z=[
0.1 0.2 0.3;
0.5 0.6 0.7;
0.8 0.9 0.10;
];


zt = reshape(z.',1,[]);
 [~,idx] = sort(zt,'ascend')

gradblue=colorGradient(hex2rgb('#0d77c3'),[1 1 1],size(z,1)*size(z,2)); %blue
gradred=colorGradient([1 1 1],hex2rgb('#fe0a0b'),size(z,1)*size(z,2)); %red 


import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('zebraTable','pdf');
formattedNumbers = arrayfun(@ (n) sprintf("%1.3f", n),z);
tb = Table(formattedNumbers);

% Conditional formatting for table entries
nRows = tb.NRows;
nCols = tb.NCols;
num=0;
for iRow = 1:nRows
    for iCol = 1:nCols
       num=num+1;
        entry = tb.entry(iRow,iCol);
        entryContent = entry.Children.Content;
%plot([1 2 3],[1 2 3],'*-','color',gradblue( idx(num),:) )
        % Provide as many cases you want based on the entry content value
        if str2double(entryContent)<0.5
            entry.Style = {BackgroundColor(rgb2hex( gradblue( idx(num),:)))};
        else
            entry.Style = {BackgroundColor(rgb2hex( gradred(idx(num),:)))};
        end
    end
end

tb.Style = {Width('100%'),...
           Border('none'),...
           ColSep('none'),...
           RowSep('none')};



%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)

代码的输出不是我想要的。

colorGradient and hex_and_rgb_v1.1.1链接在这里。

我自己也找到了问题,抽动了一下算法,达到了我要求的结果。这是执行此操作的代码。

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;
];
zt = reshape(z.',1,[]);
colour1=colorGradient(hex2rgb('#2777c4'),[1 1 1],size(z,1)*size(z,2)); 
colour2=colorGradient(hex2rgb('#5e1a2b'),[1 1 1],size(z,1)*size(z,2)); 

import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('zebraTable','pdf');
formattedNumbers = arrayfun(@ (n) sprintf("%1.3f", n),z);
tb = Table(formattedNumbers);

% Conditional formatting for table entries
nRows = tb.NRows;
nCols = tb.NCols;
num=0;
for iRow = 1:nRows
    for iCol = 1:nCols
       num=num+1;
        entry = tb.entry(iRow,iCol);
        entryContent = entry.Children.Content;

        if str2double(entryContent)>0.49
            entry.Style = {BackgroundColor(rgb2hex( colour2( sum(zt>=str2double(entryContent) ),:)))};
        else
           entry.Style = {BackgroundColor(rgb2hex( colour1( sum(zt<=str2double(entryContent)),:)))};
        end
    end
end

tb.Style = {Width('100%'),...
           Border('none'),...
           ColSep('none'),...
           RowSep('none'),...
           HAlign('center') };

tb.Width= '3in';
tb.TableEntriesVAlign = 'middle';
tb.TableEntriesHAlign = 'center';

add(rpt,tb)
close(rpt)
rptview(rpt)