Main Content

Create a Report Generator

This example shows how to create a simple report that explains and illustrates magic squares – matrices whose columns, rows, and diagonals each add up to the same number. See magic.

Note

The complete example code is included after the step-by-step instructions.

  1. Run the following command to access the supporting files used in this example.

    openExample('rptgen/MatlabReportGeneratorSupportFilesExample');
  2. Import the base classes.

    To eliminate the need to use fully qualified names of Report and DOM API objects, use these statements. For example, instead of using mlreportgen.report.Report, you can use Report.

    import mlreportgen.report.* 
    import mlreportgen.dom.* 
  3. Create a report object.

    Create the report object. Use 'magic' as its file name and 'html' as its report type.

    rpt = Report('magic','html'); 

    To customize properties that apply to the whole report, see mlreportgen.report.Report.

  4. Add a title page.

    Create a title page and specify its title, subtitle and author. Then, add the title page to the report.

    tp = TitlePage; 
    tp.Title = 'Magic Squares'; 
    tp.Subtitle = 'Columns, Rows, Diagonals: All Equal Sums'; 
    tp.Author = 'Albrecht Durer'; 
    append(rpt,tp); 

    Note

    If you are using MATLAB® version R2020a or older, replace the append function with add.

    Title page with the title "Magic Squares", subtitle "Columns, Rows, Diagonals: All Equal Sums", author "Albrecht Durer", and the date

    To customize additional title page properties, see mlreportgen.report.TitlePage.

  5. Add a table of contents.

    Add a default table of contents object to the report.

    append(rpt,TableOfContents); 
    

    Note

    If you are using MATLAB version R2020a or older, replace the append function with add.

    Table of contents that lists three chapters: "Introduction", "10 by 10 Magic Square", and "25 by 25 magic square"

    To customize the table of contents, see mlreportgen.report.TableOfContents.

  6. Add a chapter and chapter sections.

    Create a chapter object for the introduction and specify the chapter title. Add a section, add a paragraph to that section, and add that section to the chapter. Create another section and add a paragraph to it.

    ch1 = Chapter; 
    ch1.Title = 'Introduction'; 
    sec1 = Section; 
    sec1.Title = 'What is a Magic Square?'; 
    para = Paragraph(['A magic square is an N-by-N matrix '... 
    'constructed from the integers 1 through N^2 '... 
    'with equal row, column, and diagonal sums.']); 
    append(sec1,para) 
    append(ch1,sec1) 
    sec2 = Section; 
    sec2.Title = 'Albrecht Durer and the Magic Square'; 
    para = Paragraph([ ... 
    'The German artist Albrecht Durer (1471-1528) created '... 
    'many woodcuts and prints with religious and '... 
    'scientific symbolism. One of his most famous works, '... 
    'Melancholia I, explores the depressed state of mind '... 
    'which opposes inspiration and expression. '... 
    'Renaissance astrologers believed that the Jupiter '... 
    'magic square (shown in the upper right portion of '... 
    'the image) could aid in the cure of melancholy. The '... 
    'engraving''s date (1514) can be found in the '... 
    'lower row of numbers in the square.']); 
    append(sec2,para) 
    append(ch1,sec2) 

    Note

    If you are using MATLAB version R2020a or older, replace the append function with add.

    Chapter one with two sections, "What is a Magic Square" and "Albrecht Durer and the Magic Square"

    For information on customizing chapters and sections, see mlreportgen.report.Chapter and mlreportgen.report.Section respectively.

  7. Add a figure.

    Create an image of Durer in a figure window. Create the image in a MATLAB figure. Add the figure to the second section of introduction chapter and then, add the chapter to the report.

    durerImage=load(which('durer.mat'),'-mat'); 
    figure('Units','Pixels','Position',... 
    [200 200 size(durerImage.X,2)*.5 ... 
    size(durerImage.X,1)*.5 ]); 
    image(durerImage.X); 
    colormap(durerImage.map); 
    axis('image'); 
    set(gca,'Xtick',[],'Ytick',[],... 
    'Units','normal','Position',[0 0 1 1]); 
    append(sec2,Figure) 
    append(rpt,ch1) 
    close gcf 

    Note

    If you are using MATLAB version R2020a or older, replace the append function with add.

    Engraving, "Melancholia I" by Albrecht Durer

    For more information on figures, see mlreportgen.report.Figure. For more information on images, see mlreportgen.report.FormalImage.

  8. Add a table.

    Add another chapter object and specify its title. Specify the MATLAB code to create a 10-by-10 magic square. Add the results to a table and set these table properties:

    • Row and column separators

    • Table border

    • Alignment of table entries

    Then, add the table to the chapter and the chapter to the report.

    ch2 = Chapter(); 
    ch2.Title = sprintf('10 x 10 Magic Square'); 
    
    square = magic(10); 
    tbl = Table(square); 
    
    tbl.Style = {... 
        RowSep('solid','black','1px'),... 
        ColSep('solid','black','1px'),}; 
    tbl.Border = 'double'; 
    tbl.TableEntriesStyle = {HAlign('center')}; 
    
    append(ch2,tbl); 
    append(rpt,ch2); 
    

    Note

    If you are using MATLAB version R2020a or older, replace the append function with add.

    Chapter 2 has the title 10 by 10 Magic Square and contains a bordered table containing the magic square.

    For more information on tables, see mlreportgen.dom.Table.

  9. Add a MATLAB figure to a chapter.

    Add another chapter object and specify its title. Specify the MATLAB code to create a 25-by-25 magic square and a color-coded figure of the magic square. Then, create a figure object and set its height, width, and caption. Add the figure to the chapter and the chapter to the report.

    ch3 = Chapter(); 
    ch3.Title = sprintf('25 x 25 Magic Square'); 
    
    square = magic(25); 
    clf; 
    imagesc(square) 
    set(gca,'Ydir','normal')
    axis equal 
    axis tight 
    
    fig = Figure(gcf); 
    fig.Snapshot.Height = '4in'; 
    fig.Snapshot.Width = '6in'; 
    fig.Snapshot.Caption = sprintf('25 x 25 Magic Square'); 
    
    append(ch3,fig); 
    append(rpt,ch3); 
    delete(gcf) 
    

    Note

    If you are using MATLAB version R2020a or older, replace the append function with add.

    Chapter 3 has the title 25 by 25 Magic Square and contains a color-coded figure of the magic square.

    For more information on figures, see mlreportgen.report.Figure.

  10. Close and run the report.

    close(rpt)
    rptview(rpt)

The complete code is:

Run the following command to access the supporting files used in this example.

openExample('rptgen/MatlabReportGeneratorSupportFilesExample');
import mlreportgen.report.* 
import mlreportgen.dom.* 
rpt = Report('magic','html'); 

tp = TitlePage; 
tp.Title = 'Magic Squares'; 
tp.Subtitle = 'Columns, Rows, Diagonals: All Equal Sums'; 
tp.Author = 'Albrecht Durer'; 
append(rpt,tp); 
append(rpt,TableOfContents); 

ch1 = Chapter; 
ch1.Title = 'Introduction'; 
sec1 = Section; 
sec1.Title = 'What is a Magic Square?'; 
para = Paragraph(['A magic square is an N-by-N matrix '... 
'constructed from the integers 1 through N^2 '... 
'with equal row, column, and diagonal sums.']); 
append(sec1,para) 
append(ch1,sec1) 

sec2=Section; 
sec2.Title = 'Albrecht Durer and the Magic Square'; 
para = Paragraph([ ... 
'The German artist Albrecht Durer (1471-1528) created '... 
'many woodcuts and prints with religious and '... 
'scientific symbolism. One of his most famous works, '... 
'Melancholia I, explores the depressed state of mind '... 
'which opposes inspiration and expression. '... 
'Renaissance astrologers believed that the Jupiter '... 
'magic square (shown in the upper right portion of '... 
'the image) could aid in the cure of melancholy. The '... 
'engraving''s date (1514) can be found in the '... 
'lower row of numbers in the square.']); 
append(sec2,para) 
append(ch1,sec2) 

durerImage=load(which('durer.mat'),'-mat'); 
figure('Units','Pixels','Position',... 
[200 200 size(durerImage.X,2)*.5 ... 
size(durerImage.X,1)*.5 ]); 
image(durerImage.X); 
colormap(durerImage.map); 
axis('image'); 
set(gca,'Xtick',[],'Ytick',[],... 
'Units','normal','Position',[0 0 1 1]); 
append(sec2,Figure) 
append(rpt,ch1) 
close gcf 

ch2 = Chapter(); 
ch2.Title = sprintf('10 x 10 Magic Square'); 
square = magic(10); 
tbl = Table(square); 
tbl.Style = {... 
RowSep('solid','black','1px'),... 
ColSep('solid','black','1px'),}; 
tbl.Border = 'double'; 
tbl.TableEntriesStyle = {HAlign('center')}; 
append(ch2,tbl); 
append(rpt,ch2); 

ch3 = Chapter(); 
ch3.Title = sprintf('25 x 25 Magic Square'); 
square = magic(25); 
clf; 
imagesc(square) 
set(gca,'Ydir','normal') 
axis equal 
axis tight 
fig = Figure(gcf); 
fig.Snapshot.Height = '4in'; 
fig.Snapshot.Width = '6in'; 
fig.Snapshot.Caption = sprintf('25 x 25 Magic Square'); 
append(ch3,fig); 
append(rpt,ch3); 

delete(gcf) 
close(rpt)
rptview(rpt)

Note

If you are using MATLAB version R2020a or older, replace the append function with add.

See Also