Main Content

Configure Packaging of Parameter Arguments in Generated Code

When you create a reusable component as a referenced model, configure block parameters to use the same value for instances of the component or a different value for each instance.

To customize the implementation of parameter arguments in the generated code, you can configure the packaging of these parameters in the model where they are defined.

Configure the default mapping for model parameter arguments by using one of these storage classes:

  • Default — Each instance of the parameter is allocated a piece of memory in an exported structure whose attributes are defined by identifier naming rules.

  • A structured storage class — Each instance of the parameter is allocated a piece of memory as defined by the code definition of that storage class.

Individually specify the storage class for model parameter arguments by using one of these storage classes:

  • Auto — The parameter appears as an individual model function argument in the generated code. If a block in the model does not reference the parameter, it can be optimized away by the code generator.

  • Model default — Each instance of the parameter is allocated a piece of memory as defined by the code definition of the default storage class. Specify this default storage class through the model default mapping.

If you use edit-time checking for code generation, you get an error for an invalid storage class.

For a parameter argument with a non-Auto storage class:

  • On the Model block, you can specify the per-instance value as either a literal, a numeric MATLAB® variable, or a Simulink.Parameter object.

  • If the per-instance value is a Simulink.Parameter object, the object must have storage class Auto.

  • If the per-instance value is a value expression, the evaluated value of the expression is used to statically initialize the parameter.

If you do not specify a default or individual storage class for a parameter argument, the code generator applies an internal storage class, InstP, to the parameter. The Instp storage class has these properties:

  • Exported scope

  • Type naming rule is $R_InstP$M

  • Instance name is InstP$R$M

Code Generation Behavior

For single-instance referenced models, the parameter arguments reside in a standalone global structure. The top model defines the structure. The referenced model declares and uses the structure.

For multi-instance referenced models, the parameter arguments are part of a hierarchically nested structure that the top model declares and defines. The referenced model has access to its parameter values through a pointer in its self structure. The self structure points to a substructure of the structure that the top model defines.

For parameter arguments defined in the top model, if the argument does not have a storage class, it is inlined. If it has a storage class, the resulting code depends on the value of the Code interface packaging parameter:

  • Nonreusable function — The parameter is generated in a structure defined in the top model.

  • Reusable function — The parameter is generated in a structure passed as part of the first argument of the function.

To understand the behavior of instance-specific parameters in the generated code, consider this referenced model that defines two parameter arguments, gainArg and coeffArg.

Parameters tab of Code Mappings editor. The two Model Workspace Parameter Arguments, coeffArg and gainArg, are highlighted.

First consider the case where this model is configured to support only a single instance and the default mapping for model parameter arguments does not have a storage class. In the ex_arg_code_ref.h file, there is a type definition and a declaration for the instance-specific parameters.

Snippet of the code generated from the model showing the definition of a structure that contains the two parameter arguments.

When the top model references this model and supplies values for the instance-specific parameters, the code for the top model defines the instance-specific parameters by using the values provided by the top model.

Snippet from the generated code showing the definition of the parameter arguments buffer with the initial values 1.98 and 3.98.

Now consider if the referenced model is configured to support multiple instances. ex_arg_code_ref.h contains the same type definition as the single-instance, but the self for the model also contains a pointer to an instance of that data structure. There is no pointer declaration to a standalone global variable of that structure.

The top model declares a parent structure, including an instance of this substructure, and initializes the pointer in the self structure for the referenced model.

Limitations

When you configure an instance-specific parameter for code generation, these limitations apply:

  • The default mapping for model parameter arguments cannot be reused for model parameters or external parameter objects.

  • Instance-specific parameter values must be finite.

  • Parameter arguments with a non-Auto storage class are not supported for C++ code generation with a C++ class code interface.

Related Topics