Generate Code for Variant Subsystem with Child Subsystems of Different Output Signal Dimensions
This example shows how to use symbolic dimensions to generate code with preprocessor conditionals for a variant subsystem consisting of child subsystems of different output signal dimensions. The value of the variant control variable determines the active variant choice and the output signal dimensions. By changing the value of the variant control variable, you change the active variant and the output signal dimensions in the generated code. For more information on variant subsystems, see Implement Variations in Separate Hierarchy Using Variant Subsystems.
Example Model
The model slexVariantSymbolicDims
contains a Variant Subsystem
consisting of the child subsystems Subsystem
and
Subsystem1
. When the variant control variable Var
has a value of 1
, Subsystem
is the active variant.
When Var
has a value of 2
,
Subsystem1
is the active variant.
Simulate Model with Variant Subsystem of Different Output Signal Dimensions
To generate code with preprocessor conditionals, the output signal dimensions of the child subsystems must be the same during simulation. In this example, double-clicking the subsystem Activate Variant Choice
changes the active variant and the output signal dimension. When Var
equals 1
, the output signal dimension of each child subsystem is 5
. When Var
equals 2
, the output signal dimension of each child subsystem is 6
.
1. Open the example model:
openExample('slexVariantSymbolicDims')
2. On the Debug tab, select Information Overlays > Signal Dimensions.
3. Open the Variant Subsystem Block Parameters dialog box. The Variant activation time parameter is set to code compile
.
4. Open Subsystem
. In the Constant Block Parameters dialog box, the Constant value parameter is P1
.
5. Open Subsystem1
. In the Constant Block Parameters dialog box, the Constant value parameter is P2
.
6. Open the base workspace. The Simulink.Parameters
P1
and P2
are arrays with dimensions '[1,A]'
. The Simulink.Parameter
A
has a value of 5
. Var
has a value of 1
.
7. Simulate the model. Subsystem
is the active variant with an output signal dimension of 5
.
8. Double-click the masked subsystem ActivateVariant
.
9. In the base workspace, Var
is now 2
. P1
and P2
have a dimension of 6
. A
has a value of 6
.
10. Simulate the model. Subsystem1
is the active variant with an output signal dimension of 6
.
In the base workspace, A
has a Storage class of ImportedDefine(Custom)
. To use a Simulink.Parameter
object for dimension specification, it must have one of these storage classes:
Define
orImportedDefine
with header file specifiedCompilerFlag
User-defined custom storage class that defines data as a macro in a specified header file
P1
and P2
have a storage class of ImportedExtern
. A Simulink.Parameter
object that uses a Simulink.Parameter
for symbolic dimension specification must have a storage class of either ImportedExtern
or ImportedExternPointer
, or a storage class with the Data initialization property set to None
.
Generate Code
1. Open the header file slexVariantSymbolicDims_variant_defines.h
. The definition of A
is conditional upon the value of Var
.
/* Copyright 2024 The MathWorks, Inc. */
// To select variant choice during compile, define Var at compile time,
#ifndef Var
#define Var 1
#endif
#if Var == 1
#define A 5
#elif Var == 2
#define A 6
#else
#error "Variant control variable, Var, must be defined as 1 or 2"
#endif
2. Generate code.
3. Open the slexVariantSymbolicDims.h
file. The output dimension size is A
.
/* External outputs (root outports fed by signals with auto storage) */
typedef struct {
int32_T Out1[A]; /* '<Root>/Out1' */
} ExternalOutputs_slexVariantSymb;
4. Open the slexVariantSymbolicDims.c
file. If Var
equals 1
, P1
has five values. If Var
equals 2
, P2
has six values. In the Configuration Parameters dialog box, on the Code Generation > Custom Code pane, the Additional code parameter contains this code.
/* user code (top of source file) */
#if Var == 1
int32_T P1[] = { 5, 5, 5, 5, 5 };
#elif Var == 2
int32_T P2[] = { 6, 6, 6, 6, 6, 6 };
#endif
Preprocessor conditionals control the size of A
and select which array (P1
or P2)
is active in the generated code. By changing the value of Var
, you can change the size of A
and the active array.
Related Topics
- Use Symbolic Dimensions for Signal Dimensions (Embedded Coder)
- Implement Symbolic Dimensions for Array Sizes in Generated Code (Embedded Coder)