Search code examples
matlabpdfreport

Report with side by side two images and a splited table - Matlab


I am trying in the code below to generate a report with side by side two images and a splited table but I get an error. Why this error occur?

Code:

close all;
clear all;
clc;

import mlreportgen.report.*
import mlreportgen.dom.*
import mlreportgen.utils.*

Name = {'A';'B';'C';'D';'E';'A';'B';'C';'D';'E'};
codeA = [8;3;8;0;4;8;3;8;0;4];
Height = [1;8;4;7;8;8;3;1;0;4];
Weight = [6;2;1;4;5;8;3;1;1;4];
T = table(Name,codeA,Height,Weight,codeA,Height,Weight,codeA,Height,Weight);

Image1 = Image(which('coins.png'));
Image2 = Image(which('sevilla.jpg'));

rpt = Report("myPDF","pdf");
  
imgStyle = {ScaleToFit(true)};
Image2.Style = imgStyle;
Image1.Style = imgStyle;

lot = Table({Image2, ' ', Image1});

lot.entry(1,1).Style = {Width('3.2in'), Height('3in')};
lot.entry(1,2).Style = {Width('.2in'), Height('3in')};
lot.entry(1,3).Style = {Width('3.2in'), Height('3in')};
lot.Style = {ResizeToFitContents(false), Width('100%')};
add(rpt, lot);
 
chapter = Chapter("Title",'Table Report');
table = FormalTable(T);
table.Border = 'Solid';
table.RowSep = 'Solid';
table.ColSep = 'Solid';

para = Paragraph(['The table is sliced into two tables, '...
    'with the first column repeating in each table.']);
para.Style = {OuterMargin('0in','0in','0in','12pt')};
para.FontSize = '14pt';
add(chapter,para)

slicer = TableSlicer("Table",table,"MaxCols",5,"RepeatCols",1);
totcols = slicer.MaxCols - slicer.RepeatCols;
slices = slicer.slice();
for slice=slices
    str = sprintf('%d repeating column and up to %d more columns',...
        slicer.RepeatCols,totcols);
    para = Paragraph(str);
    para.Bold = true;
    add(chapter,para)
    add(chapter,slice.Table)
end

add(rpt,chapter)
close(rpt)
rptview(rpt)

Error:

*Index exceeds the number of array elements. Index must not exceed 10.

Error in try1 (line 26)

lot.entry(1,1).Style = {Width('3.2in'), Height('3in')};*


Solution

  • You define the variable

    Height = [1;8;4;7;8;8;3;1;0;4];
    

    Then you try and use the report gen function Height

    lot.entry(1,1).Style = {Width('3.2in'), Height('3in')};
    

    Because you've shadowed the Height function with a variable, MATLAB is trying to get the element of this array at index '3in', which is either nonsensical or (via some implicit ASCII conversion) is way out of range.

    Per my comment on your previous question, I think the way the documentation suggests the report gen functions are imported is bad practice. By using import mlreportgen.dom.* you are putting all of the nicely name-spaced functions from that package into the common area, and in this case it has caused an unclear clash between two things. So there are two options:

    1. Use the namespaced version of Height (and Width), if you did this with all of the report gen functions you would not need the import. The nice side-effect is you get tab-completion when typing the various functions from this package

      lot.entry(1,1).Style = {mlreportgen.dom.Width('3.2in'), mlreportgen.dom.Height('3in')};
      

      Sure, you code is longer, but it is more explicit.

      ... or ...

    2. Simply don't define a variable called Height. Rename this and everything else can stay the same.