【问题标题】:How can I export a Matlab 2D array in Conditional Formatted table in pdf using MATLAB Report Generator?如何使用 MATLAB 报告生成器将 Matlab 二维数组导出为 pdf 格式的条件格式表?
【发布时间】:2021-02-20 20:07:48
【问题描述】:

我在 Matlab 中有一些二维数组。我想在 3 Color scheme Conditional Formatted(Excel 中最常用的函数)表中以 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 和 hex_and_rgb_v1.1.1 链接在这里。

【问题讨论】:

    标签: matlab pdf matlab-figure matlab-app-designer


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多