Simulink自定义菜单栏功能

Simulink自定义菜单栏功能

  1. 使用cm对象的addCustomMenuFcn函数,添加自定义菜单
function sl_customization(cm)
cm.addCustomMenuFcn('Simulink:ToolsMenu',@getMyMenuItems)
end
  1. 自定义菜单创建函数
function schemaFcns = getMyMenuItems(~)
schemaFcns = {@getItem1};    
end
  1. 为菜单项定义模式函数
function schema = getItem1(~)
schema = sl_action_schema;
schema.label = 'PortAlignment';
schema.callback = @PortAlignment;
end
  1. 自定义菜单功能
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
  1. 最后一步,把sl_customization.m放在matlab文件的路径下,在MATLAB命令窗口输入以下命令,更新Simulink控制面板
    在这里插入图片描述

  2. 菜单栏生成效果
    在这里插入图片描述

  3. 测试工具效果
    subsystem的端口混乱,鼠标先单击subsystem模块,然后单击安装的命令。
    在这里插入图片描述
    subsystem模块的端口自动对齐,效果如下。
    在这里插入图片描述

  4. 此菜单栏功能代码如下:

%% 自定义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

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值