主要内容

ModelAdvisor.FormatTemplate

R2026b

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

描述

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

创建对象

描述

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

注意

在检查回调中使用 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 中推出