主要内容

创建一个报告生成器

这个例子展示了如何创建一个简单的报告,解释和说明了魔术方块——矩阵的列、行和对角线每个相同数量。看到魔法

请注意

完整的示例代码包括循序渐进的指示。

  1. 导入基类。

    消除需要使用完全限定名称的报告和DOM API对象,使用这些语句。例如,而不是使用mlreportgen.report.Report,你可以使用报告

    进口mlreportgen.report。*进口mlreportgen.dom。*
  2. 创建一个报告对象。

    创建报告对象。使用“魔法”文件名和“html”作为它的报告类型。

    rpt =报告(“魔法”、“html”);

    自定义属性,适用于整个报告,明白了mlreportgen.report.Report

  3. 添加一个标题页。

    创建一个标题页,并指定其标题、副标题和作者。然后,在报表中添加标题页。

    tp = TitlePage;tp。Title =“魔术方块”;tp。字幕= '列、行对角线:所有同等金额”;tp。作者=“Albrecht Durer”;追加(rpt, tp);

    标题页的标题“魔术方块”,副标题“列、行对角线:全部相等金额”、“Albrecht Durer”,作者和日期

    自定义额外的标题页属性,看看mlreportgen.report.TitlePage

  4. 添加一个目录。

    添加一个默认的目录对象。

    追加(rpt TableOfContents);

    表列出了三个章节的内容:“介绍”,“10×10魔法广场”,“25 25个魔法广场”

    自定义表的内容,明白了mlreportgen.report.TableOfContents

  5. 添加一个章,章部分。

    创建一个章对象的介绍和指定章节标题。添加一个部分中,添加一个节段,并添加部分的章节。创建另一个部分,并添加一个段落。

    ch1 =章;ch1。Title =“介绍”;sec1 =节;sec1。Title =“幻方是什么?”;帕拉=段([“幻方是一个n×n矩阵”……“由整数1到N ^ 2的……”以同样的行、列和对角线金额。”);追加(sec1 para)追加(ch1 sec1) sec2 =节; 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)

    与两个部分,第一章“幻方”和“Albrecht Durer和魔法广场”

    定制章节和部分信息,请参阅mlreportgen.report.Chaptermlreportgen.report.Section分别。

  6. 添加一个数字。

    创建一个图像杜勒的图窗口。在MATLAB中创建图像。图添加到第二部分介绍然后章,章添加到报告。

    durerImage =负载((“durer.mat”),“垫”);图(“单位”,“像素”、“位置”,…200年[200大小(durerImage.X, 2) *。5……大小(durerImage.X 1) *。5);图像(durerImage.X);colormap (durerImage.map);轴('图像');集(gca、“Xtick”, [],“Ytick”, [],…“单位”、“正常”,“位置”,[0 0 1 1]);追加(sec2图)追加(rpt ch1) gcf关闭

    Albrecht Durer雕刻,“我忧郁症”

    有关数据的更多信息,请参阅mlreportgen.report.Figure。有关图像的更多信息,请参阅mlreportgen.report.FormalImage

  7. 添加一个表。

    添加另一个对象,并指定其章标题。指定MATLAB代码来创建一个10 *幻方。将结果添加到表和表设置这些属性:

    • 行和列分隔符

    • 表边境

    • 对齐的表格条目

    然后,添加表章,这一章。

    ch2 =(章);ch2。Title = sprintf (“10 x 10幻方”);平方=魔法(10);台=表(广场);资源描述。风格= {…RowSep(“固体”、“黑色”,“1 px”),…ColSep(“固体”、“黑色”,“1 px”),};资源描述。Border = 'double'; tbl.TableEntriesStyle = {HAlign('center')}; append(ch2,tbl); append(rpt,ch2);

    第二章的标题10×10幻方和包含一个邻接表包含幻方。

    表上的更多信息,请参阅mlreportgen.dom.Table

  8. 添加一个MATLAB图一章。

    添加另一个对象,并指定其章标题。指定MATLAB代码来创建一个25-by-25幻方和幻方的彩色图。然后,创建一个图对象,并设置它的高度,宽度,和标题。图添加到这一章,这一章的报告。

    甲基=(章);甲基。Title = sprintf (“25 x 25幻方”);平方=魔法(25);clf;显示亮度图像(广场)集(gca,‘Ydir’,‘正常’)轴平等轴紧无花果=图(gcf);fig.Snapshot。身高=“4”;fig.Snapshot。宽度= ' 6 '; fig.Snapshot.Caption = sprintf('25 x 25 Magic Square'); append(ch3,fig); append(rpt,ch3); delete(gcf)

    第三章标题25 25个幻方和包含一个彩色图的幻方。

    有关数据的更多信息,请参阅mlreportgen.report.Figure

  9. 关闭和运行报告。

    关上(rpt) rptview (rpt)

完整的代码是:

进口mlreportgen.report。*进口mlreportgen.dom。*rpt =报告(“魔法”、“html”);tp = TitlePage;tp。Title =“魔术方块”;tp。字幕= '列、行对角线:所有同等金额”;tp。作者=“Albrecht Durer”;追加(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)

另请参阅