主要内容

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

生成嵌入式 Web 视图报告

R2026b

此示例说明如何使用定义报告生成器的类实例生成嵌入式 Web 视图报告,如 创建嵌入式 Web 视图报告生成器 中所述。

为嵌入式 Web 视图报告创建类

创建一个类,用于为指定 Simulink® 模型所使用的工作区和数据字典变量生成文件。

classdef SystemDesignVariables < slreportgen.webview.EmbeddedWebViewDocument
    % SystemDesignVariables: Report on variables used by a Simulink model
    %   Defines a class of report generators that produce HTML reports on
    %   the workspace and data dictionary variables used by a Simulink
    %   model. The generated report includes this information for
    %   each variable:
    %
    %   Value (if the value is a scalar, numeric value)
    %   Data Type
    %   Source (e.g, path of dictionary containing the variable)
    %   Source Type (e.g., data dictionary or base workspace)
    %   Users (path of blocks that use the variable)

    methods

        function rpt = SystemDesignVariables(reportPath, modelName)

            % Invoke the EmbeddedWebViewDocument constructor, which
            % saves the report path and model name for use by the fill
            % methods of the report.
            rpt@slreportgen.webview.EmbeddedWebViewDocument( ...
                reportPath,modelName);

            % Turn off duplicate link warnings to avoid warnings for
            % blocks that use multiple design variables.
            rpt.ValidateLinksAndAnchors = false;

            rpt.ExportOptions.IncludeMaskedSubsystems = true;
            rpt.ExportOptions.IncludeSimulinkLibraryLinks = true;
            rpt.ExportOptions.IncludeReferencedModels = true;
        end

        function fillContent(rpt)
            % Fill the Content hole in the report template with design
            % variable information. Use DOM or Report API methods to
            % create, format, add, and append content to this report.

            %% Set up report
            % Allow use of unqualified names for DOM and Report objects,
            % such as Paragraph instead of mlreportgen.dom.Paragraph and
            % TitlePage instead of mlreportgen.report.TitlePage.
            import mlreportgen.dom.*
            import mlreportgen.report.*

            % Obtain the model name, which was saved by the report
            % constructor. getExportedModels returns model names as a
            % cell array, in case a report uses multiple models.
            model = getExportModels(rpt);

            % Extract the model from the cell array. (This report uses
            % only one model.)
            model= model{1};

            % Add a title page to the report.
            add(rpt, TitlePage("Title",[model " Report"],"Author",""));

            % Find variables in the model.
            finder = slreportgen.finder.ModelVariableFinder(model);

            % Create a Variables chapter.
            ch = Chapter("Variables");

            while hasNext(finder)
                result = next(finder);
                % Create a section for the variable.
                s = Section(result.Name);

                % Add variable information to the section using
                % default reporter settings.
                reporter = getReporter(result);
                add(s,reporter);

                % Add the section to the chapter.
                add(ch,s);
            end

            % Add the chapter to the report.
            add(rpt,ch);
        end
    end
end

创建嵌入式 Web 视图报告

要生成并显示一个使用此类的报告实例,请在 MATLAB® 命令行窗口中输入以下命令。

model_name = "f14";
openExample(model_name)
rptName = sprintf('%sVariables',model_name);
rpt = SystemDesignVariables(rptName,model_name);
fill(rpt);
close(rpt);
rptview(rptName);

报告生成器从基类 slreportgen.webview.EmbeddedWebViewDocument 继承的 fill 方法将 f14 模型的 Web 视图嵌入到报告中。它还会调用 fillContent 方法,将文档窗格填充为关于模型中变量的报告。

Embedded web view report for the f14 model showing variable information and model web view with hyperlinks between variables and blocks.

有关如何导航到报告的不同部分的详细信息,请参阅 浏览嵌入式 Web 视图报告。

要在设计变量用户路径与使用设计变量的 Web 视图中的模块之间生成双向链接,请将

reporter = getReporter(result);
add(s,reporter);

与

usedByPara = Paragraph("Used By:");
usedByPara.Bold = true;
add(s, usedByPara);
users = result.Users;
nUsers = numel(users);
for u = 1:nUsers
    userLink = createElementTwoWayLink(rpt, ...
        users{u}, ...
        Paragraph(users{u}));
    add(s,userLink);
end

另请参阅

主题