Simulink自定义菜单栏功能
- 使用cm对象的addCustomMenuFcn函数,添加自定义菜单
function sl_customization(cm)
cm.addCustomMenuFcn('Simulink:ToolsMenu',@getMyMenuItems)
end
- 自定义菜单创建函数
function schemaFcns = getMyMenuItems(~)
schemaFcns = {@getItem1};
end
- 为菜单项定义模式函数
function schema = getItem1(~)
schema = sl_action_schema;
schema.label = 'PortAlignment';
schema.callback = @PortAlignment;
end
- 自定义菜单功能
function PortAlignment(~)
port_handles=get_param(gcbh,'PortHandles');
Inport_handles=port_handles.Inport;
for i=1:length(Inport_handles)
line_handles=get_param(Inport_handles(i),'Line');
src_handles=get_param(line_handles,'SrcBlockHandle');
port_pos=get(Inport_handles(i),'Position');
src_pos=get_param(src_handles,'Position');
src_len=src_pos(3)-src_pos(1);
src_width=src_pos(4)-src_pos(2);
new_pos(1)=port_pos(1)-100;
new_pos(3)=new_pos(1)+src_len;
new_pos(2)=port_pos(2)-fix(src_width/2);
new_pos(4)=port_pos(2)+fix(src_width/2);
set_param(src_handles,'Position',new_pos);
end
outport_handles=port_handles.Outport;
for i=1:length(outport_handles)
line_handles = get_param(outport_handles(i),'Line');
src_handles =get_param(line_handles,'DstBlockHandle');
port_pos=get(outport_handles(i),'Position');
src_pos=get_param(src_handles,'Position');
src_len=src_pos(3)-src_pos(1);
src_width=src_pos(4)-src_pos(2);
new_pos(1)=port_pos(1)+100;
new_pos(3)=new_pos(1)+src_len;
new_pos(2)=port_pos(2)-fix(src_width/2);
new_pos(4)=port_pos(2)+fix(src_width/2);
set_param(src_handles,'Position',new_pos);
end
SubSystems = find_system(gcs,'FindAll','On','BlockType','SubSystem');
set(SubSystems,'ContentPreviewEnabled','Off');
end
-
最后一步,把sl_customization.m放在matlab文件的路径下,在MATLAB命令窗口输入以下命令,更新Simulink控制面板
-
菜单栏生成效果
-
测试工具效果
subsystem的端口混乱,鼠标先单击subsystem模块,然后单击安装的命令。
subsystem模块的端口自动对齐,效果如下。
-
此菜单栏功能代码如下:
%% 自定义Simulink菜单栏
% 2021.01.14
% Author:
%% File Content
% 使用cm对象的addCustomMenuFcn函数,添加自定义菜单
function sl_customization(cm)
cm.addCustomMenuFcn('Simulink:ToolsMenu',@getMyMenuItems)
end
% 自定义菜单创建函数
function schemaFcns = getMyMenuItems(~)
schemaFcns = {@getItem1};
end
% 为菜单项定义模式函数
function schema = getItem1(~)
schema = sl_action_schema;
schema.label = 'PortAlignment';
% schema.userdata = 'item one';
schema.callback = @PortAlignment;
end
% 自定义菜单功能
function PortAlignment(~)
port_handles=get_param(gcbh,'PortHandles');
Inport_handles=port_handles.Inport;
for i=1:length(Inport_handles)
line_handles=get_param(Inport_handles(i),'Line');
src_handles=get_param(line_handles,'SrcBlockHandle');
port_pos=get(Inport_handles(i),'Position');
src_pos=get_param(src_handles,'Position');
src_len=src_pos(3)-src_pos(1);
src_width=src_pos(4)-src_pos(2);
new_pos(1)=port_pos(1)-100;
new_pos(3)=new_pos(1)+src_len;
new_pos(2)=port_pos(2)-fix(src_width/2);
new_pos(4)=port_pos(2)+fix(src_width/2);
set_param(src_handles,'Position',new_pos);
end
outport_handles=port_handles.Outport;
for i=1:length(outport_handles)
line_handles = get_param(outport_handles(i),'Line');
src_handles =get_param(line_handles,'DstBlockHandle');
port_pos=get(outport_handles(i),'Position');
src_pos=get_param(src_handles,'Position');
src_len=src_pos(3)-src_pos(1);
src_width=src_pos(4)-src_pos(2);
new_pos(1)=port_pos(1)+100;
new_pos(3)=new_pos(1)+src_len;
new_pos(2)=port_pos(2)-fix(src_width/2);
new_pos(4)=port_pos(2)+fix(src_width/2);
set_param(src_handles,'Position',new_pos);
end
SubSystems = find_system(gcs,'FindAll','On','BlockType','SubSystem');
set(SubSystems,'ContentPreviewEnabled','Off');
end
文章链接:https://blog.csdn.net/acknole/article/details/112630002