问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Matlab编程技巧:通过脚本获取/修改Simulink模块参数

创作时间:
作者:
@小白创作中心

Matlab编程技巧:通过脚本获取/修改Simulink模块参数

引用
CSDN
1.
https://blog.csdn.net/u013288925/article/details/103943921

在MBD(基于模型的设计)开发中,可能需要反复修改simulink模型。传统的方式是通过在模型中找到相应的模块,然后点进去修改参数。本文介绍一种通过Matlab脚本批量获取Simulink模块参数,并修改参数的方法。

相关函数

1.1 搜索出符合条件的模块、信号线、端口等

函数 Objects = find_system(System,Name,Value)

输入参数 1)System——模型名称;2)Name,Value——搜索条件的名称、值,可以输入多组名称、值

返回值 1)Objects——符合搜索条件的模块路径的元胞数组

例如,搜索出demo.slx模型中所有的Gain模块

>> GainCell = find_system('demo','BlockType','Gain') %搜索出demo.slx模型中所有的Gain模块
GainCell =
  2×1 cell 数组
    {'demo/Gain' }
    {'demo/Gain1'}

1.2 获取模块指定参数的值

函数 ParamValue = get_param(Object,Parameter)

输入参数 1)Object——模块路径,即find_system输出参数;2)Parameter——模块参数

返回值 1)ParamValue——模块参数的值

例如,获取demo/Gain模块的输出数据类型

>> OutDataTypeStr = get_param('demo/Gain','OutDataTypeStr')  %获取demo/Gain模块的输出数据类型
OutDataTypeStr =
    'Inherit: Inherit via internal rule'

1.3 设置模块指定参数的值

函数 set_param(Object,ParameterName,Value)

输入参数 1)Object——模块路径,即find_system输出参数;2)Parameter——模块参数;3)参数值

返回值 无

例如,设置demo/Gain模块的输出数据类型为single

>> set_param('demo/Gain','OutDataTypeStr','single') %设置demo/Gain模块的输出数据类型为single
>> OutDataTypeStr = get_param('demo/Gain','OutDataTypeStr')
OutDataTypeStr =
    'single'

常见问题

2.1 参数名称在脚本中对应的字符串是什么

章节1.2的示例中,Gain模块的输出数据类型是’OutDataTypeStr’,这个字符串是从哪里知道的呢?有以下两种途径获得。

1)右键模块Gain——Properties——Block Annotation,可以在左边的列表框中看到该模块的所有属性,找到相应的即可。

2)双击模块——找到填写相应参数的地方——右键What’s This?——弹出的帮助窗口拉到最下面——Block Parameter右边写的就是了

2.2 只想搜索模型顶层的模块怎么办

只想搜索出顶层系统的输入端,不想搜索子系统,可以通过find_system的’SearchDepth’参数来控制。

例如,把前文的demo创建子系统,如下图:

通过给find_system传入’SearchDepth’为1的参数,即可只搜索顶层模块:

>> InportCell = find_system('demo','BlockType','Inport') %搜索所有Inport模块
![](https://wy-static.wenxiaobai.com/chat-rag-image/13532076410181529300)
InportCell =
  4×1 cell 数组
    {'demo/In1'          }
![](https://wy-static.wenxiaobai.com/chat-rag-image/4038489135539150829)
    {'demo/In2'          }
    {'demo/Subsystem/In1'}
    {'demo/Subsystem/In2'}
>> InportCell = find_system('demo','SearchDepth',1,'BlockType','Inport') %只搜索顶层Inport模块
InportCell =
  2×1 cell 数组
    {'demo/In1'}
    {'demo/In2'}

参考资料

[1]https://ww2.mathworks.cn/help/simulink/slref/find_system.html?s_tid=doc_ta

[2]https://ww2.mathworks.cn/help/simulink/slref/get_param.html?s_tid=doc_ta

本文原文来自CSDN。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号