Simulink 是一款功能强大的仿真与编程工具。在许多应用中,我们需要编写脚本来自动完成特定任务,这时常常需要在命令行中获取或修改模块框图的参数值。

get() 与 get_param()

Simulink 提供了 get 和 get_param 两个函数来获取模块框图的参数值。

get 函数接收句柄作为输入,get_param 则接收句柄或模块路径作为输入。

注意,可以用 gcb 获取当前选中模块的路径,用 bdroot 获取顶层模型的路径。

假设 obj_h 是模块的句柄,blk_path 是模块的路径。可以通过下面的代码实现句柄与路径之间的相互转换。

blk_path = [get(obj_h,'path'),'/',get(obj_h,'name'))];
% or 
blk_path = [get_param(obj_h,'path'),'/',get_param(obj_h,'name'))];

obj_h    = get_param(blk_path, 'handle');

get 和 get_param 的用法和返回值大致相同,不同之处在于 get 可以通过句柄获取所有参数的值,即:

% get DialogParameters
get(obj_h, 'DialogParameters')

% get_param DialogParameters
get_param(obj_h, 'DialogParameters')

% get_param DialogParameters by block path
get_param(blk_path, 'DialogParameters')

% get all parameters
get(obj_h)

对话框参数(Dialog Parameters)

摘自 Simulink 的帮助页面

指定的参数结果
‘ObjectParameters’返回一个结构体数组,以指定对象(模型、模块或根)的各个参数名作为结构体中的独立字段。
‘DialogParameters’返回一个结构体数组,以模块对话框的各个参数名作为结构体中的独立字段。如果模块带有封装(mask),则返回封装参数。
参数名,例如 'BlockType'。可以指定任意模型参数、模块参数或模块对话框参数。返回指定模型参数或模块参数的值。

以 Gain 模块为例,get_param(gcb,'DialogParameters') 的返回值是一个结构体

                         Gain: [1×1 struct]
               Multiplication: [1×1 struct]
                     ParamMin: [1×1 struct]
                     ParamMax: [1×1 struct]
             ParamDataTypeStr: [1×1 struct]
                       OutMin: [1×1 struct]
                       OutMax: [1×1 struct]
               OutDataTypeStr: [1×1 struct]
                    LockScale: [1×1 struct]
                      RndMeth: [1×1 struct]
    SaturateOnIntegerOverflow: [1×1 struct]
                   SampleTime: [1×1 struct]

这些字段的值可以通过下面的方式获取

for param = fieldnames(get_param(gcb,'DialogParameters'))'
  fprintf('%s = %s\n',param{:}, mat2str(get_param(gcb, param{:})))
end

上面代码的输出是

Gain = '2'
Multiplication = 'Element-wise(K.*u)'
ParamMin = '[]'
ParamMax = '[]'
ParamDataTypeStr = 'Inherit: Inherit via internal rule'
OutMin = '[]'
OutMax = '[]'
OutDataTypeStr = 'Inherit: Inherit via internal rule'
LockScale = 'off'
RndMeth = 'Floor'
SaturateOnIntegerOverflow = 'off'
SampleTime = '-1'

IntrinsicDialogParameters 与 AlgorithmParameters

除了 DialogParameters,模块框图的参数还包括 IntrinsicDialogParameters、AlgorithmParameters 和 SecondaryAlgorithmParameters。

            DialogParameters: [1×1 struct]
   IntrinsicDialogParameters: [1×1 struct]
         AlgorithmParameters: [1×1 struct]
SecondaryAlgorithmParameters: [1×1 struct]

IntrinsicDialogParameters 是模块自身特有参数的名称/属性列表(不管该模块是否封装)。如果要获取封装或未封装模块自身特有的参数,就用它来代替 DialogParameters。例如,对于我写的一个封装子系统,其 DialogParameters 是

WrapTypeStr = '[-pi, pi) or [-1, 1)'
EnPuIn = 'off'
EnTheta = 'on'
EnThetaPu = 'off'

其 IntrinsicDialogParameters 是

ShowPortLabels = 'FromPortIcon'
BlockChoice = ''
TemplateBlock = ''
MemberBlocks = ''
Permissions = 'ReadWrite'
ErrorFcn = ''
PermitHierarchicalResolution = 'All'
TreatAsAtomicUnit = 'on'
MinAlgLoopOccurrences = 'off'
PropExecContextOutsideSubsystem = 'off'
SystemSampleTime = '-1'
RTWSystemCode = 'Inline'
RTWFcnNameOpts = 'Auto'
RTWFcnName = ''
RTWFileNameOpts = 'Auto'
RTWFileName = ''
FunctionInterfaceSpec = 'void_void'
FunctionWithSeparateData = 'off'
RTWMemSecFuncInitTerm = 'Inherit from model'
RTWMemSecFuncExecute = 'Inherit from model'
RTWMemSecDataConstants = 'Inherit from model'
RTWMemSecDataInternal = 'Inherit from model'
RTWMemSecDataParameters = 'Inherit from model'
IsSubsystemVirtual = 'off'
VariantControlMode = 'Expression'
Variant = 'off'
VariantControl = ''
LabelModeActiveChoice = ''
GeneratePreprocessorConditionals = 'off'
AllowZeroVariantControls = 'off'
PropagateVariantConditions = 'off'
ActiveVariant = ''
ActiveVariantBlock = ''
TreatAsGroupedWhenPropagatingVariantConditions = 'on'

关于 AlgorithmParameters 和 SecondaryAlgorithmParameters 没有详细的资料。我认为这些参数会影响算法的底层实现。

Add 模块的 AlgorithmParameters 是

Inputs = '++'

add 模块的 SecondaryAlgorithmParameters 是

AccumDataTypeStr = 'Inherit: Inherit via internal rule'
RndMeth = 'Floor'
SaturateOnIntegerOverflow = 'off'

注意:在 Simulink Coder 编译之后,Divide 和 Multiply 模块的部分 ‘CollapseMode’ 参数、Sqrt 模块的 ‘AlgorithmType’ 参数可能会从 ‘AlgorithmParameters’ 中消失。我还没有找到出现这种情况的原因。


0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注