メインコンテンツ

魔方陣についてのレポートの作成

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

メモ: MATLAB® version R2020a 以前を使用している場合は、append 関数呼び出しを add に置き換えます。

1. 完全修飾クラス名を使用せずに済むよう、レポートと DOM 名前空間をインポートします。

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

2. レポート オブジェクトを作成します。ファイル名として "magic"、レポートのタイプとして "html" を使用します。レポート全体に適用されるプロパティをカスタマイズするには、mlreportgen.report.Reportを参照してください。

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

3. タイトル ページを作成して、タイトル、サブタイトル、作成者を指定します。その後、タイトル ページをレポートに追加します。タイトル ページのプロパティをさらにカスタマイズするには、mlreportgen.report.TitlePageを参照してください。

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

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

4. 既定の目次オブジェクトをレポートに追加します。目次をカスタマイズするには、mlreportgen.report.TableOfContentsを参照してください。

append(rpt,TableOfContents); 

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

5. 序章の章オブジェクトを作成して、章タイトルを指定します。節を追加し、節に段落を追加して、節を章に追加します。別の節を作成して、節に段落を追加します。章と節のカスタマイズの詳細については、mlreportgen.report.Chaptermlreportgen.report.Sectionをそれぞれ参照してください。

ch1 = Chapter; 
ch1.Title = "Introduction"; 
sec1 = Section; 
sec1.Title = "What is a Magic Square?"; 
para = Paragraph(join(["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(join([ ... 
"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) 

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

6. Figure ウィンドウでデューラーのイメージを作成します。MATLAB Figure でイメージを作成します。この Figure を序章の第 2 節に追加し、この章をレポートに追加します。Figure の詳細については、mlreportgen.report.Figureを参照してください。イメージの詳細については、mlreportgen.report.FormalImageを参照してください。

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 

Engraving, "Melancholia I" by Albrecht Durer

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

  • 行と列の区切り

  • テーブルの境界線

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

次に、テーブルを章に追加し、章をレポートに追加します。テーブルの詳細については、mlreportgen.dom.Tableを参照してください。

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

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

8.別の章オブジェクトを追加して、タイトルを指定します。25 行 25 列の魔方陣と色分けされた魔方陣の Figure を作成する MATLAB コードを指定します。次に、figure オブジェクトを作成して、高さ、幅、および表題を設定します。Figure を章に追加し、章をレポートに追加します。Figure の詳細については、mlreportgen.report.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) 

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

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

close(rpt)
rptview(rpt)

参考