simulink系列之模型接口表生成及自动连线脚本
总目录
simulink系列之汽车应用层信号处理
第一章 simulink信号处理——debounce
第二章 simulink接口表生成及自动连线脚本
目录
前言
一、simulink接口表生成脚本
1.使用方法:
二、模型整理连线脚本
1.使用方法:
总结
前言
本系列主要围绕作者采用simulink进行日常软件开发过程中的实战和思考。话不多说,直接开干。
对于汽车应用层开发人员来说,几乎可以说日常开发接触到最多的东西就是各种报文信号了。因此,本文主要分享一下应用层软件报文信号处理之debounce,作为本系列的第一篇文章。
一、simulink接口表生成脚本
该脚本主要用来生成simulink应用层输入输出信号的基本信息。
1.使用方法:
使用对象:一般是针对最外层的子系统,当然也可以是内部的子系统
使用方法:打开simulink模型,点击一个子系统,然后运行脚本即可。
注意:如果需要在最终的接口表中生成特定的字段,需要稍微修改一下脚本内容。
脚本如下:
% 脚本1:生成iflist
% filepath: extract_ports_info.m
% 获取当前选中的子系统
subsys = gcs;% 只查找当前子系统下的一级 Inport/Outport(不递归)
inports = find_system(subsys, 'SearchDepth', 1, 'BlockType', 'Inport');
outports = find_system(subsys, 'SearchDepth', 1, 'BlockType', 'Outport');% 初始化结果
names = {};
types = {};
directions = {};% 处理 Inport
for i = 1:length(inports)blk = inports{i};names{end+1,1} = get_param(blk, 'Name');types{end+1,1} = get_param(blk, 'OutDataTypeStr');directions{end+1,1} = 'Input';
end% 处理 Outport
for i = 1:length(outports)blk = outports{i};names{end+1,1} = get_param(blk, 'Name');types{end+1,1} = get_param(blk, 'OutDataTypeStr');directions{end+1,1} = 'Output';
end% 生成表格
T = table(names, types, directions, ...'VariableNames', {'Name', 'DataType', 'Direction'});disp(T);
writetable(T, 'ports_info.xlsx');
二、模型整理连线脚本
该脚本主要用来整理子系统联系不美观,弯曲的问题。
1.使用方法:
使用对象:一般是针对各个子系统
使用方法:打开simulink模型,点击一个子系统,然后运行脚本即可。
注意:如果选中的子系统连线已经交错等情况时,输出的结果可能不是最佳状态,需要手动协助调整。
脚本如下:
% 脚本2:自动连线
clear;
clc;
port_handles = get_param(gcbh,'PortHandles');
inport_handles = port_handles.Inportfor i = 1:length(inport_handles)line_handles = get_param(inport_handles(i),'Line');
src_handles = get_param(line_handles,'SrcBlockHandle');port_pos = get_param(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);endoutport_handles = port_handles.Outportfor i = 1:length(outport_handles)line_handles = get_param(outport_handles(i),'Line');
src_handles = get_param(line_handles,'DstBlockHandle');port_pos = get_param(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
总结
以上就是本篇要分享的内容,觉得有用可以关注我后续文章。