Simulink is a powerful simulating and programming tool. In many applications, we need to write scripts to automate specific tasks. At this time, we often need to obtain or modify the parameter values of the block diagram in command line.
get() and get_param()
Simulink provides get
and get_param
functions to get the parameter values of the block diagram.
The get
function receives the handle input value, and get_param
receives the handle or block path input value.
Note, you can use
gcb
to obtain the path of currently selected block, andbdroot
to obtain the path of the base diagram.
Assuming obj_h
is the block’s handle, blk_path
is the block’s path. You can use the following codes to achieve the conversion between handle and 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');
The usage and return value of get
and get_param
are basically the same, the difference is that get
can get all parameter values through the handle. i.e.,
% 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
From the Simulink’s help page
Specified Parameter | Result |
---|---|
‘ObjectParameters’ | Returns a structure array with the parameter names of the specified object (model, block, or root) as separate fields in the structure. |
‘DialogParameters’ | Returns a structure array with the block dialog box parameter names as separate fields in the structure. If the block has a mask, the function instead returns the mask parameters. |
Parameter name, e.g., 'BlockType' . Specify any model or block parameter, or block dialog box parameter. | Returns the value of the specified model or block parameter. |
Take the Gain
block for example, the return value of get_param(gcb,'DialogParameters')
is a struct
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]
The value of these fields can be obtained by
for param = fieldnames(get_param(gcb,'DialogParameters'))'
fprintf('%s = %s\n',param{:}, mat2str(get_param(gcb, param{:})))
end
The output of above code is
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 and AlgorithmParameters
In addition to DialogParameters, the parameters of the block diagram also include IntrinsicDialogParameters, AlgorithmParameters, and SecondaryAlgorithmParameters.
DialogParameters: [1×1 struct]
IntrinsicDialogParameters: [1×1 struct]
AlgorithmParameters: [1×1 struct]
SecondaryAlgorithmParameters: [1×1 struct]
The IntrinsicDialogParameters is list of names/attributes of block-specific parameters (regardless of whether the block is masked or unmasked). Use instead of DialogParameters
if you want block-specific parameters for masked or unmasked blocks. e.g., for a masked subsystem I’ve written, the DialogParameters
is
WrapTypeStr = '[-pi, pi) or [-1, 1)'
EnPuIn = 'off'
EnTheta = 'on'
EnThetaPu = 'off'
The IntrinsicDialogParameters is
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'
There’s no detailed information on AlgorithmParameters and SecondaryAlgorithmParameters. I think these parameters are parameters that can affect the underlying implementation of the algorithm.
The AlgorithmParameters of an Add
block is
Inputs = '++'
The SecondaryAlgorithmParameters of an add
block is
AccumDataTypeStr = 'Inherit: Inherit via internal rule'
RndMeth = 'Floor'
SaturateOnIntegerOverflow = 'off'
Note: Some of the ‘CollapseMode’ parameter of Divide and Multiply block, the ‘AlgorithmType’ parameter of Sqrt block may be removed from ‘AlgorithmParameters’ after Simulink Coder build. I haven’t found the reason for such behavior.
0 Comments