主要内容

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

创建 Simulink Report Generator 报告

Simulink®Report Generator™ 报告 API 包括用于查找和格式化模型和仿真数据的对象。您可以使用这些对象与 MATLAB®报告 API 和 DOM API 对象一起创建 MATLAB 程序,以生成关于 Simulink 模型和仿真的报告。此示例展示了如何使用 Simulink 报告 API 和 MATLAB 报告 API 来创建一个 MATLAB 程序。该程序生成有关 Simulink 模型内容的报告。报告包含以下节:

  • 标题页面

  • 目录

  • 根系统章节 - 包含根模块图和根图中每个模块的属性。

  • 子系统章节 - 包含模型中每个子系统的图和模块属性。

  • Stateflow® 图章节 - 包含模型中每个图的图和图对象属性。

创建报告

要避免使用报告、查找器和 DOM API 函数的完全限定名称,请使用以下语句。例如,您可以使用 slreportgen.finder.BlockFinder,而不是使用 BlockFinder

import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.report.*

加载 sf_car 模型。

model = load_system("sf_car");

使用 Simulink 报告构造函数 slreportgen.report.Report 创建一个报告对象来保存报告的内容。您必须完全限定构造函数的名称,以将其与 MATLAB 报告构造函数 mlreportgen.report.Report 区分开来。将报告的名称设置为 sdd_,后跟模型的 Name 属性的值。

rpt = slreportgen.report.Report("sdd_" + get_param(model,"Name"),"pdf");

添加标题页

使用标题页报告器构造函数 mlreportgen.report.TitlePage 创建标题页。将 TitleSubtitleAuthor 属性设置为分别指定报告标题、副标题和作者的字符数组。

使用图报告器构造函数 slreportgen.report.Diagram 创建模型的图快照。该报告器生成了该模型模模块图。要将此图像包含在报告标题页中,请将图报告器分配给标题页报告器的 Image 属性。然后,将标题页添加到报告。

tp = TitlePage;
tp.Title = upper(get_param(model,"Name"));
tp.Subtitle = "System Design Description";
tp.Author = "MathWorks";
tp.Image = Diagram(model);
append(rpt,tp);

Report title page with title, subtitle, author, and model diagram.

添加目录

使用目录报告器构造函数 mlreportgen.report.TableOfContents 为报告创建目录。将目录报告器添加到报告中。

toc = TableOfContents;
append(rpt,toc);

Table of contents showing chapters and subsections

为根系统添加章节

使用章节构造函数 mlreportgen.report.Chapter,根据 TitleContent 属性创建一个章节。报告器对章节标题进行编号,并生成章节页眉、页脚和页码。

在章节中添加模型图报告器。该报告器返回您添加到该章节的模块图的图像。

ch = Chapter("Title","RootSystem");
append(ch,Diagram(model));

Block diagram image displaying below chapter name RootSystem

为根系统中的每个模块添加章节

使用模块查找器构造函数 slreportgen.finder.BlockFinder 为根图创建一个模块查找器。然后,使用 find 方法返回一个 slreportgen.finder.BlockResult 对象数组,每个对象都包含一个模块。

循环遍历模块结果对象。对于每个结果,构建一个部分报告器 mlreportgen.report.Section,以生成一个编号报告部分。将节 Title 属性设置为其报告的模块的名称。

将当前模块的结果添加到部分报告器,将部分报告器 Content 属性设置为 slreportgen.report.SimulinkObjectProperties 报告器。该报告器生成当前模块属性的表格,并将其添加到该部分。将每个小节添加到父章节。然后,将该章节添加到报告中。

blkFinder = BlockFinder(model);
blocks = find(blkFinder);
for block = blocks
    section = Section("Title", ...
       strrep(block.Name,newline," "));
    append(section,block);
    append(ch,section);
end
append(rpt,ch);

Subsections of the report with tables of properties inserted in each subsection

为子系统添加章节

在模型中为子系统创建一个章节,并在每个子系统中创建模块。

ch = Chapter("Title","Subsystems");

在模型中找到子系统。系统图查找器 slreportgen.finder.SystemDiagramFinder 返回一个 slreportgen.finder.DiagramResult 对象数组,每个对象都包含一个 Diagram 报告器,该报告器创建子系统的快照。

sysdiagFinder = SystemDiagramFinder(model);
sysdiagFinder.IncludeRoot = false;

Chapter titled Subsystems, followed by subsection titled Engine, which contains a snapshot of subsystem diagram, and another subsection titled Ti which contains a table of properties

使用 while 循环为每个子系统创建一个章节部分。查找每个子系统中的块和模块元素。为每个章节部分添加一个模块元素表,并将每个部分添加到章节中。然后,将该章节添加到报告中。

while hasNext(sysdiagFinder)
    system = next(sysdiagFinder);
    section1 = Section("Title",system.Name);
    append(section1,system);
    
    blkFinder1 = BlockFinder(system);
    elems = find(blkFinder1);
    for elem = elems
       section2 = Section("Title",...
           strrep(elem.Name,newline," "));
       append(section2,elem);
       append(section1,section2);
    end
    append(ch,section1);
end
append(rpt,ch);

Subsection titled Vehicle, which contains a block diagram, and another subsection titled brake torque which contains a table of properties

为 Stateflow 图和对象添加章节

创建一个章节。使用图查找器 slreportgen.finder.ChartDiagramFinder 在模型中查找 Stateflow 图。使用 while 循环为每个图添加小节。使用图元素查找器 slreportgen.finder.StateflowDiagramElementFinder 查找每个图中的元素,并将它们添加到小节中。然后,将节添加到章中,将章添加到报告中。

ch = Chapter("Title", "Stateflow Charts");

chdiagFinder = ChartDiagramFinder(model);
while hasNext(chdiagFinder) 
   chart = next(chdiagFinder); 
   section = Section("Title",chart.Name);
   append(section,chart);

   objFinder = StateflowDiagramElementFinder(chart);
   sfObjects = find(objFinder);
   for sfObj = sfObjects
       title = sfObj.Name;
       if isempty(title)
          title = sfObj.Type;
       end
       objSection = Section("Title",title);
       append(objSection,sfObj);
       append(section,objSection);
   end
   append(ch,section);
end
append(rpt,ch);

Chapter title Stateflow Charts with subsections for each Stateflow chart, followed by tables of properties

生成报告

关闭报告,运行报告,然后关闭模型。

close(rpt);
rptview(rpt);
close_system(model);

另请参阅