主要内容

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

通过定义函数格式化报告

此示例说明如何使用文档对象模型 (DOM) 和报告 API 编写代码来创建和格式化报告。当您不想创建和使用自定义模板时,可以使用脚本和自定义函数来生成和格式化报告。当报告比较简单,不需要复杂的格式或内容布局时,脚本和函数很有用。对于布局复杂的报告,编写生成内容和布局的脚本和函数不如使用自定义模板高效且易于维护。请参阅创建 HTML 和 PDF 模板创建 Microsoft Word 模板在 Microsoft Word 模板中添加空位

在此示例中,您执行一个生成报告的自定义函数 rptpoprptpop 定义文档部件,使用其他自定义函数添加文本和绘图,然后将内容追加到报告中。

检查自定义函数

打开 rptpop 函数。该函数可以创建 HTML、DOCX 或 PDF 格式的报告。

首先,rptpop 创建报告、定义页眉和样式,并使用以下代码将页眉追加到报告中。

function rptpop(doctype)
%RPTPOP DOM-based report example
%
%   This example is based on the MATLAB example: "Predicting the US
%   Population." That example was generated using MATLAB Publish. This
%   example is generated using the DOM and Report APIs. This example is
%   intended to illustrate a simple use of the APIs, one that does not
%   entail use of holes or templates.
%
%   rptpop() generates an HTML version of the report.
% 
%   rptpop(doctype) generates a report of the specified type:
%   "html", "docx", or "pdf".

%   Copyright MathWorks, 2013-2025

% These statements eliminate the need to qualify the names of DOM and
% Report API objects in this function, e.g., you can refer to
% mlreportgen.report.Report simply as Report.
import mlreportgen.dom.*
import mlreportgen.report.*

if nargin < 1
    doctype = "html";
end

% Create a report.
rpt = Report("population", doctype);

% Create a title for the report.
h = Heading(2, "Predicting the US Population");

% Create a border format object that effectively draws a light gray
% rule under the title.
b = Border();
b.BottomStyle = "single";
b.BottomColor = "LightGray";
b.BottomWidth = "1pt";

% Set the style of the title programmatically to be dark orange with 
% a light gray rule under it.
%
% Note that we could have achieved the same effect
% by defining a title style in a template associated with this object
% and setting the heading's StyleName property to the name of the 
% template-defined style, e.g.,
%
% h.StyleName = 'Title';
%
% This would allow us to change the appearance of the title without 
% having to change the code.
%
% Note also that we append the border and color format objects to the
% heading's existing Style array. This avoids overwriting the
% heading's existing OutlineLevel format.
h.Style = [h.Style {Color("DarkOrange"), b}];

% Append the heading to the document.
append(rpt, h);

为了在不使用自定义模板的情况下向报告追加内容,rptpop 定义了两个嵌套函数 addBodyParaaddPlotaddBodyPara 嵌套函数将指定的字符串文本作为段落追加到报告中。

function addBodyPara(rpt, text)
% Format report text and append it to a document.
import mlreportgen.dom.*;
p = Paragraph(text);
p.Style = {FontFamily("Arial"), FontSize("10pt")};
append(rpt, p);
end

addPlot 函数将图窗追加到报告中。

function addPlot(rpt)
% Add a snapshot of the current figure to the report with the Figure
% reporter.
import mlreportgen.report.*;

figReporter = Figure(gcf);

% Set image height and width.
figReporter.Scaling = "custom";
figReporter.Width = "5in";
figReporter.Height = "4in";

% Append image to document.
append(rpt, figReporter);

% Delete plot figure window.
delete(gcf);

end

rptpop 使用这些嵌套函数将文本和图窗添加到报告中。例如,rptpop 使用以下代码在报告开头添加四个段落:

addBodyPara(rpt, "This example shows that using polynomials of " + ...
    "even modest degree to predict the future by extrapolating " + ...
    "data is a risky business.");
addBodyPara(rpt, "This example is older than MATLAB. It started as " + ...
    "an exercise in ""Computer Methods for Mathematical " + ...
    "Computations"", by Forsythe, Malcolm and Moler, published " + ...
    "by Prentice-Hall in 1977.");
addBodyPara(rpt, "Now, MATLAB and Handle Graphics make it " + ...
    "much easier to vary the parameters and see the results, " + ...
    "but the underlying mathematical principles are unchanged.");
addBodyPara(rpt, "Here is the US Census data from 1900 to 2000.");

然后,rptpop 通过定义股票数据、使用 MATLAB® 创建绘图并调用 addPlot 函数,将第一个绘图添加到报告中。

% Time interval
t = (1900:10:2000)';

% Population
p = [75.995 91.972 105.711 123.203 131.669 ...
    150.697 179.323 203.212 226.505 249.633 281.422]';

% Plot
plot(t,p,"bo");
axis([1900 2020 0 400]);
title("Population of the U.S. 1900-2000");
ylabel("Millions");

% Add the plot to the document (see addPlot function below).
addPlot(rpt);

rptpop 使用类似的代码向报告中添加更多段落和绘图。

执行 rptpop 并查看报告。

要指定报告的格式类型,请在 rptpop 函数输入参量中输入格式类型。例如,通过执行以下代码将报告生成为 HTML 文档。

rptpop("html")

要尝试其他格式,请将函数输入更改为 "pdf""docx"

另请参阅

| | | |

主题