主要内容

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

按创建顺序将图窗添加到报告中

此示例说明如何按创建顺序将图窗添加到报告中。您可以使用报告资源管理器或报告 API 来创建报告、生成图窗并按创建顺序追加图窗。

使用报告资源管理器

要使用报告资源管理器按创建顺序将图窗追加到报告中,需要指定生成这些图窗的组件,对每个图窗捕获一个快照,然后将该快照追加到报告中。在报告资源管理器中打开 figure_order_ex.rpt 文件。

setedit("figure_order_ex.rpt")

The Report Explorer for the example report. The report is selected. The Report Explorer shows the components in the left pane, the components that you can add in the middle pane, and the report properties in the right pane.

为了生成图窗,报告使用了一个 Evaluate MATLAB Expression 组件。当您生成报告时,组件会创建五个图窗。Eval 节点(位于 Report 节点下)包含以下代码:

for i = 1:5
   figure(i)
   plot(rand(1,100));
   title(num2str(i))
end

生成图窗后,报告会捕获图窗。为此,报告使用一个 For Loop 组件对图窗数量进行循环。在报告资源管理器中,For Loop 组件会针对递增变量的每个值计算其每个子组件。点击标记为 RPTGEN_LOOP = 1:1:5 的组件。组件创建一个名为 RPTGEN_LOOP 的循环变量。

The Workspace Variable section in the Report Explorer after selecting the For Loop component. The Variable name property is set to RPTGEN_LOOP.

对于循环变量的每个值,For Loop 组件会执行一个 Evaluate MATLAB Expression 组件来选择图窗。要查看所执行的代码,请点击左窗格中的 Report > for RPTGEN_LOOP = 1:1:15 > Eval:

figure(RPTGEN_LOOP)

由于 figure 数组按创建时间对图窗进行索引,因此循环首先选择最早的图窗,最后选择最新的图窗。

为了对选定的图窗进行操作,For Loop 组件使用一个 Figure Loop 子组件,其中包括图窗属性设置为 Current figure onlyFigure Loop 组件随后执行一个 Figure Snapshot 组件,该组件对图窗进行快照并将其追加到报告中。

报告对每个快照重复此过程。要生成报告,请点击文件 > 报告

使用报告 API

要使用 API 按创建顺序将图窗追加到报告中,您需要创建一个报告,生成图窗并将其存储在数组中,将每个图窗捕获到 mlreportgen.report.Figure 对象中,然后将每个对象追加到报告中。

首先,创建一个 html 报告。

rpt = mlreportgen.report.Report("myReport","html");

然后,生成图窗并将其存储在图形对象数组中。由于 for 循环在创建图窗时将其添加到数组中,因此数组元素的索引表示其创建顺序。

myFigures = gobjects(5,1);
for i = 1:numel(myFigures)
    myFigures(i) = figure(i);
    myFigures(i).Visible = "off";
    plot(rand(1,100));
    title(num2str(i));
end

最后,为每个图窗创建 mlreportgen.report.Figure 对象,然后根据索引将图窗追加到报告中。

for i = 1:numel(myFigures)
    reportFig = mlreportgen.report.Figure(myFigures(i));
    append(rpt,reportFig);
end

close(rpt)

另请参阅

|

主题