User`s guide

25 Create Model Advisor Checks
25-8
1
In your working directory, update the sl_customization.m file, as
shown below. This file registers and creates the check registration function
defineModelAdvisorChecks, which also registers the check callback function
SimpleCallback. The function SimpleCallback creates a check that finds
Constant blocks that have numeric values. SimpleCallback uses the Model
Advisor format template.
function sl_customization(cm)
% --- register custom checks
cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
% --- defineModelAdvisorChecks function
function defineModelAdvisorChecks
mdladvRoot = ModelAdvisor.Root;
rec = ModelAdvisor.Check('exampleCheck');
rec.Title = 'Check Constant block usage';
rec.TitleTips = ['Fail if Constant block value is a number; Pass if' ...
' Constant block value is a letter'];
rec.setCallbackFcn(@SimpleCallback,'None','StyleOne')
mdladvRoot.publish(rec, 'Demo');
% --- SimpleCallback function that checks constant blocks
function result = SimpleCallback(system)
mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
result = {};
all_constant_blk=find_system(system,'LookUnderMasks','all',...
'FollowLinks','on','BlockType','Constant');
blk_with_value=find_system(all_constant_blk,'RegExp','On','Value','^[0-9]');
ft = ModelAdvisor.FormatTemplate('ListTemplate');
ft.setInformation(['This check looks for constant blocks that'...
'use numeric values']);
if ~isempty(blk_with_value)
ft.setSubResultStatusText(['Check has failed. The following '...
'Constant blocks have numeric values:']);
ft.setListObj(blk_with_value);
ft.setSubResultStatus('warn');
ft.setRecAction('Parameterize the constant block');
mdladvObj.setCheckResultStatus(false);
else
ft.setSubResultStatusText(['Check has passed. No constant blocks'...
' with numeric values were found.']);
ft.setSubResultStatus('pass');
mdladvObj.setCheckResultStatus(true);
end
ft.setSubBar(0);
result{end+1} = ft;
2
Close the Model Advisor and your model if either are open.