文件

创建报表生成器

此示例显示了如何创建一个简单的报告,该报告解释并说明了列,行和对角线的魔方 - 矩阵均添加到相同的数字(参见魔法MATLAB中的功能参考®文档)。

笔记

完整的示例代码包含在逐步说明之后。

  1. 导入基类。

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

    进口mlreportgen.report。*进口mlreportgen.dom。*
  2. 创建报表对象。

    创建报表对象。采用“魔法”作为其文件名和'html'作为其报告类型。

    RPT =报告('魔术','HTML');

    要自定义应用于整个报表的属性,请参见mlreportgen.report.Report

  3. 添加标题页。

    创建标题页面并指定其标题,字幕和作者。然后,将标题页添加到报告。

    tp = TitlePage;tp。Title = '魔法方块';tp。Subtitle = '列,行,对角线:所有相等的和';tp。Author = 'Albrecht Durer';add (rpt, tp);

    要自定义其他标题页属性,请参阅mlreportgen.report.TitlePage

  4. 添加一个目录。

    将默认目录对象添加到报告中。

    add (rpt, TableOfContents);

    要自定义内容表,请参阅mlreportgen.report.tableofContents.

  5. 添加一个章节和章节。

    为引言创建一个章节对象,并指定章节标题。添加一个章节,在该章节中添加一个段落,然后将该章节添加到章节中。创建另一个部分并添加一个段落。

    ch1 =章;ch1.title ='介绍';sec1 =节;sec1.title ='什么是魔法广场?';para =段落(['魔术方是从整数1到n ^ 2'...'构造的n个vir-n矩阵'...,具有相等的行,列和对角线。');添加(秒,段)添加(ch1,sec1)sec2 =部分;sec2.title ='albect dureer和magic square';Para =段([...'德国艺术家Albrecht Dure(1471-1528)创建了“...”许多木刻和与宗教和“...”科学象征主义的印刷。他最着名的作品之一'...“忧郁症I,探索抑郁的精神状态”……这与灵感和表达相反。 '... '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)

    有关自定义章节和部分的信息,请参阅mlreportgen.report.chere.mlreportgen.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”,[],…'Units','normal','Position',[0 0 1 1]);add(rpt,ch1) close gcf

    有关数字的更多信息,请参阅mlreportgen.report.figure..有关图像的更多信息,请参见mlreportgen.report.FormalImage

  7. 添加表格。

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

    • 行和列分隔符

    • 表格边框

    • 表项对齐

    然后,将表格添加到章节,将章节添加到报告。

    ch2 =章节();ch2.title = sprintf('10 x 10 magic square');平方=魔法(10);台=表(广场);tbl.style = {... Rowsep('solid','black','1px'),... colsep('solid','black','1px'),};tbl.border ='double';tbl.tableentriesttyle = {halign('center')};add (ch2(资源);添加(RPT,CH2);

    有关表的详细信息,请参阅mlreportgen.dom.Table

  8. 将MATLAB图添加到章节。

    添加另一章对象并指定其标题。指定MATLAB代码以创建一个25×25的魔方广场和魔方的颜色编码图。然后,创建图形对象并设置其高度,宽度和标题。将图形添加到章节和章节中的报告。

    甲基=(章);ch3.title = sprintf('25 x 25魔方');平方=魔法(25);clf;imagesc(square) set(gca,'Ydir','normal') axis equal axis tight图=图(gcf);fig.Snapshot.Height =“4”;fig.Snapshot.Width = ' 6 ';fig.Snapshot.Caption = sprintf('25 x 25 Magic Square');add (ch3、无花果);add (rpt, ch3); delete(gcf)

    有关数字的更多信息,请参阅mlreportgen.report.figure.

  9. 关闭并运行报告。

    关闭(RPT)RPTVIEW(RPT)

完整的代码是:

进口mlreportgen.report。*进口mlreportgen.dom。*RPT =报告('魔术','HTML');tp = TitlePage;tp。Title = '魔法方块';tp。Subtitle = '列,行,对角线:所有相等的和';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)

也可以看看