Main Content

Simulink.VariantBankCoderInfo Class

Namespace: Simulink

Specify code generation properties for variant parameter bank

Since R2023a

Description

A variant parameter, which is a Simulink.VariantVariable object, helps you to vary the values of block parameters in a Simulink® model conditionally. To improve the efficiency and readability of the code generated for a model that uses variant parameters by Embedded Coder®, you can use a variant parameter bank. A variant parameter bank is a Simulink.VariantBank object that groups variant parameters with the same variant conditions into a structure in generated code and uses a pointer mechanism to access the active set of values from the structure array. To specify code generation properties for the variant parameter bank structure array and pointer variable, create objects of the Simulink.VariantBankCoderInfo class.

You can control code placement by setting the HeaderFile and DefinitionFile properties of the Simulink.VariantBankCoderInfo object. Specify the memory section to place the values in the compiled code by providing code statements in the PreStatement and PostStatement properties. Variant parameter banks allow you to specify these attributes separately for the variant parameter bank structure array and pointer variable using the AllChoicesCoderInfo and ActiveChoiceCoderInfo properties (since R2025a). To apply the same code generation properties for both the structure array and the pointer variable, set the AllChoicesCoderInfo and ActiveChoiceCoderInfo properties to the same Simulink.VariantBankCoderInfo object.

Note

You can add Simulink.VariantBank and Simulink.VariantBankCoderInfo objects to the base workspace or the Design Data section of a data dictionary either programmatically or from the Add menu in the Model Explorer.

When using both Specification and Bank properties for a Simulink.VariantVariable object, the specification object must have storage class set to Auto and it must not use symbolic dimensions. For a variant parameter bank, use a Simulink.VariantBankCoderInfo object to specify code generation attributes.

Creation

Description

varParamBankCoderInfo = Simulink.VariantBankCoderInfo returns a Simulink.VariantBankCoderInfo object with default property values.

example

varParamBankCoderInfo = Simulink.VariantBankCoderInfo(PropertyName=Value) returns a Simulink.VariantBankCoderInfo object and sets properties using one or more name-value arguments.

example

Properties

expand all

Name of the header file where the code generator places the type definition and data declarations of the variant parameter bank, specified as a string or character vector.

Example: 'variant_params.h'

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Name of the definition file where the code generator places the structure array containing the variant parameter choice values, specified as a string or character vector.

Example: 'variant_params.c'

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Code to insert before the variant parameter bank data definition or declarations in the header or definition file, respectively, specified as a string or character vector.

Example: '#pragma data_seg(".mydata")'

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Code to insert after the variant parameter bank data definition or declarations in the header or definition file, respectively, specified as a string or character vector.

Example: '#pragma end'

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Type qualifier to apply to the variable with which the Simulink.VariantBankCoderInfo object is associated, specified as "None", "Const", "Volatile", or "ConstVolatile".

The table shows the combinations of type qualifiers that are not supported when using the AllChoicesCoderInfo and ActiveChoiceCoderInfo properties of a variant parameter bank object.

ActiveChoiceCoderInfoAllChoicesCoderInfo
NoneConst
VolatileConst
NoneConstVolatile
VolatileConstVolatile

Attributes:

GetAccess
public
SetAccess
public

Examples

collapse all

Create a variant parameter bank.

EngineParams = Simulink.VariantBank(Name="EngineParams", ...
  Description="Engine parameters", ...
  VariantConditions={'V == EngType.Small','V == EngType.Big'});

Create a Simulink.VariantBankCoderInfo object to specify code generation properties for the variant parameter bank structure array that will represent EngineParams in the generated code. Use the name of the object to set the AllChoicesCoderInfo property of the EngineParams bank object.

EngineParamsAllChoicesCoderInfo = Simulink.VariantBankCoderInfo(HeaderFile="vparams.h", ...
  DefinitionFile="vparams.c", ...
  PreStatement='#pragma data_seg(.vparamdata)',PostStatement="#pragma end", ...
  Qualifier="Const");
EngineParams.AllChoicesCoderInfo = "EngineParamsAllChoicesCoderInfo";

Create a second Simulink.VariantBankCoderInfo object to specify code generation properties for the pointer variable used by the parameter bank. Use the name of the object to set the ActiveChoicesCoderInfo property of the EngineParams bank object.To apply the same code generation properties for both the structure array and the pointer variable, set the AllChoicesCoderInfo and ActiveChoiceCoderInfo properties to the same Simulink.VariantBankCoderInfo object.

EngineParamsActiveChoicesCoderInfo = Simulink.VariantBankCoderInfo(HeaderFile="vparams_ptr.h", ...
  DefinitionFile="vparams_ptr.c", ...
  PreStatement="#pragma data_seg(.pointerdata)",PostStatement="#pragma end", ...
  Qualifier="Const");
EngineParams.ActiveChoiceCoderInfo = "EngineParamsActiveChoicesCoderInfo";

Create a variant control variable V using the Simulink.VariantControl class. The Value property of this object allows you to select an active value for a variant parameter and the ActivationTime property allows you to specify a variant activation time. Use this variable to specify the variant condition expression for the choice values of a variant parameter.

Create two variant parameter objects K1 and K2 with variant activation time startup and associate them with the variant parameter bank EngineParams.

V = Simulink.VariantControl(Value=EngType.Small, ...
   ActivationTime="startup");
K1 = Simulink.VariantVariable(Choices={"V == EngType.Small",3,"V == EngType.Big",6}, ...
    Bank="EngineParams");
K2 = Simulink.VariantVariable(Choices={"V == EngType.Small", [3, 6, 9],"V == EngType.Big",[5, 10, 15]}, ...
    Bank="EngineParams");

Use the variant parameters K1 and K2 in your model and generate code from the model using Embedded Coder®. For information on how to generate code, see Generate Code Using Embedded Coder (Embedded Coder).

Because K1 and K2 have variant activation time set to startup, the code contains a structure EngineParams. The name of this structure is the same as the Name property of the parameter bank object. The structure definition is in the header file vparams.h and the structure array is in the definition file vparams.c.

/* vparams.h */ 
/* Variant parameter bank: EngineParams, for system '<Root>' */
/* Engine parameters */
typedef struct {
  real_T K2[3];   
  real_T K1;   
} EngineParams;

/* Variant parameter bank: EngineParams */
/* Variant parameter bank section */
#pragma data_seg(.vparamdata)
extern const EngineParams EngineParams_ptr_impl[2];
#pragma end

A structure array EngineParams_ptr_impl of type EngineParams contains choice values of K1 and K2. The number of elements in the array depends on the number of variant conditions that you specify in the VariantConditions property of the parameter bank object.

/* vparams.c */ 
#pragma data_seg(.vparamdata)
const EngineParams EngineParams_ptr_impl[2] = {
  {
    /* Variable: K2
     * Referenced by: '<Root>/Gain1'
     */
    { 5.0, 10.0, 15.0 },

    /* Variable: K1
     * Referenced by: '<Root>/Gain'
     */
    6.0
  }, {
    /* Variable: K2
     * Referenced by: '<Root>/Gain1'
     */
    { 3.0, 6.0, 9.0 },

    /* Variable: K1
     * Referenced by: '<Root>/Gain'
     */
    3.0
  }
};
#pragma end    

Similarly, the header and definition files, vparams_ptr.h and vparams_ptr.c, contain the declaration and definition for the pointer variable.

/* vparams_ptr.h */
/* Variant parameter bank section */
#pragma data_seg(.pointerdata)
extern const EngineParams *EngineParams_ptr;
#pragma end
/* vparams_ptr.c */
#pragma data_seg(.pointerdata)
const EngineParams *EngineParams_ptr = &EngineParams_ptr_impl[1];
#pragma end

The var_param_bank_initialize function initializes the structure pointer variable EngineParams_ptr. The code uses the pointer variable to access the active value set from the array based on variant conditions.

/* model.c */

/* Model step function */
void var_param_bank_step(void)
{
 /* Outport: '<Root>/Out2' incorporates:
  *  Gain: '<Root>/Gain1'
  *  Inport: '<Root>/Input1' 
  */
  var_param_bank_Y.Out2[0] = EngineParams_ptr->K2[0] * var_param_bank_U.Input1;
  var_param_bank_Y.Out2[1] = EngineParams_ptr->K2[1] * var_param_bank_U.Input1;
  var_param_bank_Y.Out2[2] = EngineParams_ptr->K2[2] * var_param_bank_U.Input1;

 /* Outport: '<Root>/Out1' incorporates:
  *  Gain: '<Root>/Gain'
  *  Inport: '<Root>/Input'
  */
var_param_bank_Y.Out1 = EngineParams_ptr->K1 * var_param_bank_U.Input;
}

/* Model initialize function */
void var_param_bank_initialize(void)
{
 /* Variant Parameters startup activation time */
if (V == EngType_Big) {
    EngineParams_ptr = &EngineParams_ptr_impl[0U];
  } else if (V == EngType_Small) {
    EngineParams_ptr = &EngineParams_ptr_impl[1U];
  }
 var_param_startupVariantChecker();
}

Tips

To modify a Simulink.VariantBankCoderInfo object, double-click the object to open the Simulink.VariantBankCoderInfo dialog box.

Variant bank coder info dialog box

Extended Capabilities

expand all

Version History

Introduced in R2023a

expand all