Main Content

Simulink.VariantBank Class

Namespace: Simulink

Group all variant parameter values in structure array in generated code

Since R2023a

Description

Use the Simulink.VariantBank class to group variant parameters (Simulink.VariantVariable objects) with the same variant conditions into a structure in generated code called a variant parameter bank.

To add a variant parameter to a variant parameter bank, set the Bank property of the Simulink.VariantVariable object to the name of the Simulink.VariantBank object. To specify code generation properties for a variant parameter bank, use the Simulink.VariantBankCoderInfo class.

When you generate code using Embedded Coder®, Simulink.VariantVariable objects that are part of the same variant parameter bank appear in a structure. This structure is named according to the Name property of the parameter bank object. The code contains an array of this structure type and groups the choice values from the variant parameters based on the variant conditions specified for the parameter bank. Each set of values becomes an element of the structure array. The code uses a pointer variable to access the active set of values from the structure array. The code initializes this pointer based on variant conditions in the model_initialize function.

Variant parameter bank switching using a pointer variable avoids copying the choice values into the main program memory and improves the efficiency and readability of the generated code. Additionally, you can set code generation properties for the variant parameter bank, which allows you to customize code placement and specify the memory section to place the parameter values in the compiled code.

For information on code generation with variant parameter banks, see Extended Capabilities.

Note

You can use variant parameter banks only with variant parameters that have the variant activation time set to startup.

You must have Embedded Coder to switch variant parameter banks in generated code.

You must define a variant parameter bank in the same data source (base workspace or data dictionary) as the variant parameters grouped by it. You can add Simulink.VariantBank or 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.

Creation

Description

varparambank = Simulink.VariantBank returns a Simulink.VariantBank object with default property values.

example

varparambank = Simulink.VariantBank(Name=Value) returns a Simulink.VariantBank object and sets properties using one or more name-value arguments.

example

Properties

expand all

Name of the variant parameter bank, specified as a string or character vector.

In the code generated using Embedded Coder, this property is the name of the structure that represents the variant parameter bank.

Example: "EngineParams"

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Description of the variant parameter bank, specified as a string or character vector.

In the code generated using Embedded Coder, this description appears as a comment for the variant parameter bank structure type.

Example: "Engine parameters"

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

List of variant conditions for the variant parameter bank, specified as a cell array of strings. These conditions must match the variant conditions of the Simulink.VariantVariable objects in this parameter bank, except any default variant choice specified using the (default) keyword.

When you use Embedded Coder, the generated code groups the choice values from the variant parameters in the parameter bank based on these variant conditions. Each set of these grouped values becomes an element of a structure array. During model initialization, Simulink® selects the active set of parameter values from the structure array based on the variant condition that evaluates to true.

Note

Variant parameter banks do not support the (default) variant condition in the VariantConditions property.

Example: {'V == 1', 'V == 2'}

Attributes:

GetAccess
public
SetAccess
public

Data Types: cell

Name of a Simulink.VariantBankCoderInfo object, specified as a string or character vector. You can use this object to specify code generation attributes for the variant parameter bank, including filenames for code placement and memory section to place parameter values.

Note

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.

Example: 'vparambankcoderinfo'

Attributes:

GetAccess
public
SetAccess
public

Data Types: string | char

Examples

collapse all

This example shows you how to group variant parameters in the generated code by adding them to the same variant parameter bank (Simulink.VariantBank).

Create a variant parameter bank.

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

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. To use variant parameter banks, the activation time must be set up to startup. 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 and associate them with the variant parameter bank EngineParams.

Use the name of the variant parameter bank to set the Bank property of a Simulink.VariantVariable object. The generated code groups the Simulink.VariantVariable objects that share the same Bank property in the same structure.

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');

To additionally specify code generation properties for a variant parameter bank, use the Simulink.VariantBankCoderInfo class.

Create a variant parameter bank.

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

To specify code generation properties for the variant parameter bank, create an object of the Simulink.VariantBankCoderInfo class.

You can control code placement by specifying the HeaderFile and DefinitionFile properties. You can specify the memory section to place the parameter values in the compiled code by providing code statements in the PreStatement and PostStatement properties.

EngineParamsCoderInfo = Simulink.VariantBankCoderInfo(HeaderFile='vparams.h', ...
  DefinitionFile='vparams.c', ...
  PreStatement='#pragma data_seg(.mydata)', ...
  PostStatement="#pragma end");

Use the name of the Simulink.VariantBankCoderInfo object to set the BankCoderInfo property of the variant bank object.

EngineParams.BankCoderInfo = 'EngineParamsCoderInfo';

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');

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>' */
typedef struct {
real_T K2[3];                        /* Variable: K2
                                     * Referenced by: '<Root>/Gain1'
                                     */
real_T K1;                           /* Variable: K1
                                     * Referenced by: '<Root>/Gain'
                                     */
} EngineParams;

/* Variant parameter bank: EngineParams */

/* Variant parameter bank section */
#pragma data_seg(.mydata)

extern EngineParams EngineParams_ptr_impl[2];
extern EngineParams *EngineParams_ptr;

#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(.mydata)
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
  }
};

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();
}

Limitations

  • Variant parameter banks do not support code generation for external mode operation over tcpip or serial protocols. These model configuration parameter settings in the Code Generation > Interface category correspond to this mode — External mode (Simulink Coder) parameter set to On and Transport layer (Simulink Coder) parameter set to tcpip or serial.

  • Variant parameter banks do not support model hierarchies that contain referenced models.

  • Variant parameters that are part of a variant parameter bank do not support AUTOSAR code generation.

  • Software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulations are not supported for models that use variant parameter banks.

Tips

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

Variant bank dialog box

Extended Capabilities

Version History

Introduced in R2023a