Matlab skills (c) acquisition / Simulink signal line parameter modification via script

Previous blog "Matlab skills (b) get bulk / modify Simulink block parameters", shows that if the acquisition by matlab script batch / modify Simulink module parameters. Specific can refer https://blog.csdn.net/u013288925/article/details/103943921
the Simulink signal line also has a number of attributes, such as name and is associated Simulink Signal Obj. This article describes several Simulink modification function signal line, and also illustrates the parameter modification signal line via a script.

1 correlation function

1.1 Matching module searched, a signal line ports

function Objects = find_system(System,Name,Value)
Input parameters 1) System-- model name; 2) Name Name, Value-- search condition, value, can enter a list of names, values,
return value Cellular Array 1) Objects-- meet the conditions of route search module

This function and Matlab skills (b) is exactly the same, only the input parameters of special, value 'FindAll' parameter must be 'on', 'Type' parameter must be a 'Line'. In this case, the function returns a handle all of the signal lines. For example, if the following Simulink blocks:
Here Insert Picture Description
Run phrase find_system above results are as follows:

>> SignalHandle = find_system(gcs,'FindAll','on','Type','Line')

SignalHandle =

   42.0001
   43.0001
   41.0001
   40.0001

Function returns a handle to an array of signal lines thereof. If the signal line has been named, find_system parameters can be coupled in the 'Name' property to locate the handle piece of the signal lines.

1.2 Get a property value of the signal line

function V = get(H,‘PropertyName’)
Input parameters 1) H-- handle signal line, i.e. find_system output parameters; 2) PropertyName-- signal line properties
return value 1) the value of the attribute signal line V--

如果’PropertyName’不写,则函数返回含有该信号线句柄所有的参数值的结构体。如果不知道属性的字符串表示,也可以通过这种方式查看。以find_system输出的第一个句柄为例,如下所示

>> SignalProperties = get(SignalHandle(1))

SignalProperties = 

  包含以下字段的 struct:

                   DataLogging: 0
           DataLoggingNameMode: 'Use signal name'
               DataLoggingName: ''
       DataLoggingDecimateData: 0
         DataLoggingDecimation: '2'
         DataLoggingSampleTime: '-1'
    DataLoggingLimitDataPoints: 0
          DataLoggingMaxPoints: '5000'
                     TestPoint: 0
                  StorageClass: 'Auto'
       RTWStorageTypeQualifier: ''
     MustResolveToSignalObject: 0
                    SourcePort: 'In2:1'
             SignalObjectClass: 'Simulink.Signal'
                  SignalObject: []
                     CoderInfo: []
                  DocumentLink: ''
         ShowPropagatedSignals: 0
       TaskTransitionSpecified: 0
              TaskTransitionIC: '0'
           ExtrapolationMethod: 'Use global setting'
            TaskTransitionType: 'Use global setting'
          UserSpecifiedLogName: ''
             SignalPropagation: 'off'
                          Path: ''
                          Name: ''
                           Tag: ''
                   Description: ''
                          Type: 'line'
                        Parent: 'demo'
                        Handle: 42.0001
               HiliteAncestors: 'none'
               RequirementInfo: ''
                      FontName: 'auto'
                      FontSize: -1
                    FontWeight: 'auto'
                     FontAngle: 'auto'
                      Selected: 'off'
                   SegmentType: 'trunk'
                 SrcPortHandle: 25.0001
                SrcBlockHandle: 5.0001
                 DstPortHandle: 32.0001
                DstBlockHandle: 10.0001
                        Points: [2×2 double]
                    LineParent: -1
                  LineChildren: []
           SignalNameFromLabel: ''
                     Connected: 'on'

1.3 设置信号线某个属性的值

函数 set(H,‘PropertyName’,PropertyValue)
输入参数 1)H——信号线句柄,即find_system输出参数;2)PropertyName——信号线属性;3)PropertyValue——该属性的值
返回值

如果想要把第一个信号线的名称设置为“Signal1”,可以通过以下脚本:

>> set(SignalHandle(1),'Name','Signal1')

那么模型中的信号线就会自动加上名字了:
Here Insert Picture Description

2 实例应用

通常来说,对于未命名的信号线,很难直接定位到它的句柄,因为它们的属性默认基本上都是一样的。但是实际情况中,我们只需要找到连接在特定模块上的信号线就行了,因此可以通过与信号线相连接的模块来顺腾摸瓜定位到信号线句柄。
譬如,在下图的模型中,如果要找到两个顶层Inport连接的信号线,将其命名为和Inport相同的名字并关联Simulink Signal Obj,可以通过以下几步:
Here Insert Picture Description
1)通过fing_system获取Inport模块路径
2)获取Inport模块输出端口句柄
3)Inport模块输出端口句柄即为信号线源端口(SrcPortHandle),以此获取信号线句柄
4)set函数设置信号线名称和关联Simulink Signal Obj
脚本如下:

InportCell = find_system(bdroot,'SearchDepth',1,'BlockType','Inport');  %获取顶层Inport模块路径
for i = 1:length(InportCell)  
    InportName = get_param(InportCell{i},'Name');  %输入模块名称
    InportHandle = get_param(InportCell{i},'Handle');  %信号线句柄
    PortHandle = get(InportHandle,'PortHandles');  %端口句柄
    OutportHandle = PortHandle.Outport;  %输出端口句柄
    LineHandle = find_system(bdroot,'SearchDepth',1,'FindAll','on','Type','Line','SrcPortHandle',OutportHandle);  %通过输出端口句柄定位到信号线句柄
    set(LineHandle,'Name',InportName)  %设置信号线名称为输入模块名称
    set(LineHandle,'MustResolveToSignalObject',1)   %设置信号线关联Simulink Signal Obj
end

运行脚本后,就自动完成了名称设置和关联Simulink Signal Obj,可以看到模型中信号线的名字和左边的小叉子:
Here Insert Picture Description
以上所有代码均在Matlab2018a/win10环境下测试通过。

Released three original articles · won praise 0 · Views 41

Guess you like

Origin blog.csdn.net/u013288925/article/details/104065140