Main Content

Report Generator の作成

この例では、魔方陣を説明し図解する単純なレポートの作成方法を示します。魔方陣とは、列、行および対角のいずれについても、合計が同じ数字になる行列です。magic を参照してください。

メモ

完全なコードの例は、詳細な手順の後に含まれます。

  1. 次のコマンドを実行して、この例で使用されているサポート ファイルにアクセスします。

    openExample('rptgen/MatlabReportGeneratorSupportFilesExample');
  2. 基底クラスをインポートします。

    レポート API オブジェクトと DOM API オブジェクトの完全修飾名を使用する必要をなくすには、次のステートメントを使用します。たとえば、mlreportgen.report.Report を使用する代わりに、Report を使用できます。

    import mlreportgen.report.* 
    import mlreportgen.dom.* 
  3. レポート オブジェクトを作成します。

    レポート オブジェクトを作成します。ファイル名として 'magic' を、レポートのタイプとして 'html' を使用します。

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

    レポート全体に適用されるプロパティをカスタマイズするには、mlreportgen.report.Report を参照してください。

  4. タイトル ページを追加します。

    タイトル ページを作成して、ページのタイトル、サブタイトル、作成者を指定します。その後、タイトル ページをレポートに追加します。

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

    メモ

    MATLAB® version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

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

    タイトル ページのプロパティをさらにカスタマイズするには、mlreportgen.report.TitlePage を参照してください。

  5. 目次を追加します。

    既定の目次オブジェクトをレポートに追加します。

    append(rpt,TableOfContents); 
    

    メモ

    MATLAB version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

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

    目次をカスタマイズするには、mlreportgen.report.TableOfContents を参照してください。

  6. 章と節を追加します。

    序章の章オブジェクトを作成して、章タイトルを指定します。節を追加し、節に段落を追加して、節を章に追加します。別の節を作成して、節に段落を追加します。

    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) 

    メモ

    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"

    章と節のカスタマイズの詳細については、mlreportgen.report.Chaptermlreportgen.report.Section をそれぞれ参照してください。

  7. Figure を追加します。

    Figure ウィンドウでデューラーのイメージを作成します。MATLAB Figure でイメージを作成します。この Figure を序章の第 2 節に追加し、この章をレポートに追加します。

    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 

    メモ

    MATLAB version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

    Engraving, "Melancholia I" by Albrecht Durer

    Figure の詳細については、mlreportgen.report.Figure を参照してください。イメージの詳細については、mlreportgen.report.FormalImage を参照してください。

  8. テーブルを追加します。

    もう 1 つ章オブジェクトを追加して、タイトルを指定します。10 行 10 列の魔方陣を作成する MATLAB コードを指定します。結果をテーブルに追加して、次のテーブル プロパティを設定します。

    • 行と列の区切り

    • テーブルの境界線

    • テーブル エントリの配置

    次に、テーブルを章に追加し、章をレポートに追加します。

    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); 
    

    メモ

    MATLAB version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

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

    テーブルの詳細については、mlreportgen.dom.Table を参照してください。

  9. MATLAB Figure を章に追加します。

    もう 1 つ章オブジェクトを追加して、タイトルを指定します。25 行 25 列の魔方陣と色分けされた魔方陣の Figure を作成する MATLAB コードを指定します。次に、figure オブジェクトを作成して、その高さ、幅、および表題を設定します。Figure を章に追加し、章をレポートに追加します。

    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) 
    

    メモ

    MATLAB version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

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

    Figure の詳細については、mlreportgen.report.Figure を参照してください。

  10. レポートを閉じて実行します。

    close(rpt)
    rptview(rpt)

完全なコードは次のとおりです。

次のコマンドを実行して、この例で使用されているサポート ファイルにアクセスします。

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)

メモ

MATLAB version R2020a 以降を使用している場合は、関数 appendadd に置き換えます。

参考