Main Content

Simulink.VariantBankCoderInfo Class

Namespace: Simulink

Specify code generation properties for variant parameter bank

Since R2023a

Description

Use the Simulink.VariantBankCoderInfo class to specify code generation properties for Simulink.VariantBank objects. You can use this class to customize code placement and specify the memory section to place variant parameter values in compiled code.

To specify code generation properties for a variant parameter bank, create an object of the Simulink.VariantBankCoderInfo class. Use the name of this object to set the BankCoderInfo property of the Simulink.VariantBank object.

Note

You cannot specify storage class using the Specification property of a Simulink.VariantVariable object if the object is part of a variant parameter bank.

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

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

example

varParamBankCoderInfo = Simulink.VariantBankCoderInfo(Name=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

Examples

collapse all

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

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

Version History

Introduced in R2023a