主要内容

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

ModelAdvisor.FormatTemplate

创建用于格式化模型顾问结果的模板

说明

ModelAdvisor.FormatTemplate 对象定义了一个类,用于在模型顾问的分析结果窗格中格式化检查结果,从而在检查中提供统一的外观和风格。您可以使用这些对象将分析结果格式化为表格或列表。

创建对象

描述

obj = ModelAdvisor.FormatTemplate(type) 创建一个 ModelAdvisor.FormatTemplate 类的对象,其中 type 是一个字符向量,用于指定模板的格式类型,可以是 ListTemplateTableTemplate。要在分析结果窗格中显示格式化结果,请将结果对象返回给模型顾问。

注意

在检查回调中使用 ModelAdvisor.FormatTemplate 类。

示例

输入参量

全部展开

ModelAdvisor.FormatTemplate 的类型。ListTemplate 将结果格式化为列表,用于简单的列表或结果。TableTemplate 将结果格式化为表格,用于结构化的多属性数据。

对象函数

addRow向模型顾问分析结果中添加表行
setCheckText在模型顾问分析结果中添加检查的描述
setColTitles在模型顾问分析结果中添加表列标题
setInformation在模型顾问分析结果中添加子检查的描述
setListObj在模型顾问分析结果中添加模型对象的超链接列表
setRecAction在模型顾问分析结果中添加建议操作文本
setRefLink在模型顾问分析结果中添加“另请参阅”部分和链接
setSubBar在模型顾问分析结果的子检查结果之间添加线条
setSubResultStatusText在模型顾问分析结果中,在复选框或子复选框状态下方添加文本
setSubResultStatus在模型顾问分析结果中,为检查或子检查结果添加状态
setSubTitle在模型顾问分析结果中添加子检查的标题
setTableInfo将数据添加到模型顾问分析结果的表格中
setTableTitle在模型顾问分析结果中向表添加标题

示例

全部折叠

此示例展示了如何使用 slcustomization.m 文件中的 ModelAdvisor.FormatTemplate 类,将检查结果格式化为模型顾问结果窗格中的表格和列表。该示例识别模型中的所有块,并使用两个格式化模板对象以两种格式显示它们。

function sl_customization(cm)
% Register custom Model Advisor checks
cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
end

% Define and register Model Advisor checks
function defineModelAdvisorChecks
% Create and configure the check
rec = ModelAdvisor.Check("mathworks.example.SampleDetailStyle");
rec.Title = "Sample check for Model Advisor using the ModelAdvisor.FormatTemplate";
setCallbackFcn(rec,@SampleDetailStyleCallback,"None","DetailStyle");

% Publish the check
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.publish(rec,"My_Demo");
end

% Callback function for the custom check
function [ResultDescription] = SampleDetailStyleCallback(system,CheckObj)
% Find all blocks in the system
allBlocks = find_system(system);

% Format the results using templates
ResultDescription = getFormattedTemplate(allBlocks);

% Set the formatted result details in the Model Advisor
CheckObj.setResultDetails(ResultDescription);
end

% Helper function to format results as a table and a list
function [ResultDescription] = getFormattedTemplate(allBlocks)
ResultDescription = {};

%% --- Table Template ---
% Create a table template formatting object
ft1 = ModelAdvisor.FormatTemplate("TableTemplate");
setCheckText(ft1,"Find and report all blocks in the model.");
setSubTitle(ft1,"Table of Blocks");
setInformation(ft1,"This table lists all blocks found in the model.");
setRefLink(ft1, {{"Standard 1 reference"},{"Standard 2 reference"}});
setTableTitle(ft1,"Blocks in the Model");
setColTitles(ft1,{"Index","Block Name"});

if numel(allBlocks) <= 1
    % No blocks found (only the model itself)
    setSubResultStatus(ft1,"Warn");
    setSubResultStatusText(ft1,"The model does not contain blocks.");
    setRecAction(ft1,{"Add blocks to the model."});
else
    % Add each block to the table
    setSubResultStatus(ft1,"Pass");
    setSubResultStatusText(ft1,"The model contains blocks.");
    for idx = 2:numel(allBlocks) % Skip the model itself (index 1)
        addRow(ft1,{idx-1,allBlocks{idx}});
    end
end

% Add the table template to the results
ResultDescription{end+1} = ft1;

%% --- List Template ---
% Create a list template formatting object
ft2 = ModelAdvisor.FormatTemplate("ListTemplate");
setSubTitle(ft2,"List of Blocks");
setInformation(ft2,"This list shows all blocks found in the model.");
setRefLink(ft2,{{"Standard 1 reference"},{"Standard 2 reference"}});
setSubBar(ft2,false); % Suppress the separating line

if numel(allBlocks) <= 1
    setSubResultStatus(ft2,"Warn");
    setSubResultStatusText(ft2,"The model does not contain blocks.");
    setRecAction(ft2,{"Add blocks to the model."});
else
    setSubResultStatus(ft2,"Pass");
    setSubResultStatusText(ft2,"The model contains blocks.");
    setListObj(ft2,allBlocks(2:end)); % List only blocks, skip model itself
end

% Add the list template to the results
ResultDescription{end+1} = ft2;
end

版本历史记录

在 R2009a 中推出