Model Advisor "Fix" Button Disabled in Toolbar but functional via "Fix this Check"

6 次查看(过去 30 天)
I have a functional model advisor custom check with a "fix" solution implemented (also functional). However the "fix" button is disabled in the toolbar, but I can right-click the failed check and click "fix this check", and the fix solution works as expected. How do I get the toolbar enabled?
All of the help I see on the topic uses a style type of "DetailStyle" whereas I am using "StyleOne" (this is older code I am using in a newer version of matlab). I would like to avoid changing the style type.
My fix is implented as follows:
if self.HasFix
myAction = ModelAdvisor.Action;
myAction.setCallbackFcn(@self.Fix);
myAction.Name = self.FixName;
myAction.Description = self.FixDescription;
rec.setAction(myAction);
end
Everything works, just the toolbar "fix" button is disabled for some reason!

回答(1 个)

Githin George
Githin George 2024-11-26
Hi Natalie,
I was able to come across the function “setActionEnable” to enable the “fix” icon in the toolbar for the custom Model Advisor check which you have authored.
On a high level, the function can be used inside your “StyleOne” check as follows:
mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
mdladvObj.setActionEnable(true);
You can take a look at the full sample code that I’ve written below:
function sl_customization(cm)
% register custom checks
cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
end
% -----------------------------
% defines Model Advisor Checks
% -----------------------------
function defineModelAdvisorChecks
% Create ModelAdvisor.Check object and set properties.
rec = ModelAdvisor.Check('com.mathworks.sample.StyleOne');
rec.Title = 'Check whether block names appear below blocks';
rec.TitleTips = 'Check position of block names';
rec.setCallbackFcn(@SampleStyleOneCallback,'None','StyleOne');
% Create ModelAdvisor.Action object for setting fix operation.
myAction = ModelAdvisor.Action;
myAction.setCallbackFcn(@ActionCB);
myAction.Name='Make block names appear below blocks';
myAction.Description='Click the button to place block names below blocks';
rec.setAction(myAction);
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.publish(rec, 'My_Demo');
end
function result = SampleStyleOneCallback(system)
ft = ModelAdvisor.FormatTemplate('TableTemplate');
setTableTitle(ft,{'Blocks in Model'});
setColTitles(ft,{'Index','Block Name'});
allBlocks = find_system(system);
for inx = 2:length(allBlocks)
addRow(ft,{inx-1,allBlocks(inx)});
end
result = ft;
mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
mdladvObj.setActionEnable(true);
end
% -----------------------------
% This action callback function changes the location of block names.
% -----------------------------
function result = ActionCB(taskobj)
mdladvObj = taskobj.MAObj;
checkObj = taskobj.Check;
resultDetailObjs = checkObj.ResultDetails;
for i=1:numel(resultDetailObjs)
% take some action for each of them
block=Simulink.ID.getHandle(resultDetailObjs(i).Data);
set_param(block,'NamePlacement','normal');
end
result = ModelAdvisor.Text('Changed the location such that the block name is below the block.');
mdladvObj.setActionEnable(false);
end
I’ve found the following resources useful regarding the “StyleOne” Model Advisor checks and you may want to refer to the same:
The following documentation explain the input and outputs for the various “callback” styles:
Documentation for “SetActionEnable” function:
Refer the “Object Functions” section in the documentation for ModelAdvisor.FormatTemplate. Some of these pages have examples of “StyleOne” Model Advisor callbacks:

类别

Help CenterFile Exchange 中查找有关 Create Model Advisor Checks 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by