1

I have some 2D arrays in Matlab. And I wanted to export this array in pdf in 3 Colour scheme Conditional Formatted (Mostly used function in Excel) Table as shown below. How can I do that in MATLAB?

Conditial Formating

I have written a script to do that. But I can't debug what I am doing wrong.

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)

The output of the code is not according to my desire.

The colorGradient and hex_and_rgb_v1.1.1 are linked here.

Masood Salik
  • 119
  • 1
  • 1
  • 10

1 Answers1

0

I myself have figured out the problem and twitched the algorithm and achieved my required results. Here is the code to do it.

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)
Masood Salik
  • 119
  • 1
  • 1
  • 10