This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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 the magic function reference in the MATLAB® documentation).

Note

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

  1. 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.* 
  2. 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.

  3. 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'; 
    add(rpt,tp); 

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

  4. Add a table of contents.

    Add a default table of contents object to the report.

    add(rpt,TableOfContents); 
    

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

  5. 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.']); 
    add(sec1,para) 
    add(ch1,sec1) 
    sec2 = Section; 
    sec2.Title = 'Albrect 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.']); 
    add(sec2,para) 
    add(ch1,sec2) 

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

  6. 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]); 
    add(sec2,Figure) 
    add(rpt,ch1) 
    close gcf 

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

  7. 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')}; 
    
    add(ch2,tbl); 
    add(rpt,ch2); 
    

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

  8. 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'); 
    
    add(ch3,fig); 
    add(rpt,ch3); 
    delete(gcf) 
    

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

  9. Close and run the report.

    close(rpt)
    rptview(rpt)

The complete code is:

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'; 
add(rpt,tp); 
add(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.']); 
add(sec1,para) 
add(ch1,sec1) 

sec2=Section; 
sec2.Title = 'Albrect 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.']); 
add(sec2,para) 
add(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]); 
add(sec2,Figure) 
add(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')}; 
add(ch2,tbl); 
add(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'); 
add(ch3,fig); 
add(rpt,ch3); 

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

See Also