主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

创建关于幻方的报告

此示例展示了如何创建一个解释和说明幻方报告的报告。幻方阵是列、行和对角线相加结果相同的矩阵。有关详细信息,请参阅magic

注:如果您使用的是 MATLAB® 版本 R2020a 或更早版本,请将 append 函数调用替换为 add

1.导入 Report 和 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.在图窗窗口中创建 Durer 的图像。在 MATLAB 图窗中创建图像。将图窗添加到引言章节的第二部分,然后将该章节添加到报告中。有关图窗的更多信息,请参阅 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.添加另一个章节对象并指定标题。指定 MATLAB 代码来创建一个 10×10 的幻方。将结果添加到表并设置以下表属性:

  • 行和列分隔线

  • 表边框

  • 表条目的对齐

然后,将表添加到章节中,将章节添加到报告中。有关表的更多信息,请参阅 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.添加另一个章节对象并指定标题。指定 MATLAB 代码来创建一个 25×25 的幻方和一个幻方的颜色编码图窗。然后,创建一个图窗对象,并设置高度、宽度和标题。将图窗添加到章节中,将章节添加到报告中。有关图窗的更多信息,请参阅 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)

另请参阅