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.
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
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
.
Open the example model
slexVariantSymbolicDims
.On the Debug tab, select Information Overlays > Signal Dimensions.
Open the Variant Subsystem Block Parameters dialog box. The Variant activation time parameter is set to
code compile
.Open
Subsystem
. In the Constant Block Parameters dialog box, the Constant value parameter isP1
.Open
Subsystem1
. In the Constant Block Parameters dialog box, the Constant value parameter isP2
.Open the base workspace. The
Simulink.Parameters
P1
andP2
are arrays with dimensions'[1,A]'
. TheSimulink.Parameter
A
has a value of5
.Var
has a value of1
.Simulate the model.
Subsystem
is the active variant with an output signal dimension of5
.Double-click the masked subsystem
ActivateVariant
.In the base workspace,
Var
has a value of2
.P1
andP2
have a dimension of6
.A
has a value of6
.Simulate the model.
Subsystem1
is the active variant with an output signal dimension of6
.
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
In the base workspace, 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
Open the header file
slexVariantSymbolicDims_variant_defines.h
. The definition ofA
is conditional upon the value ofVar
./* Copyright 2016 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
Generate code.
Open the
slexVariantSymbolicDims.h
file. The output dimension size isA
./* External outputs (root outports fed by signals with auto storage) */ typedef struct { int32_T Out1[A]; /* '<Root>/Out1' */ } ExternalOutputs_slexVariantSymb;
Open the
slexVariantSymbolicDims.c
file. IfVar
equals1
,P1
has five values. IfVar
equals2
,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 which array,P1
orP2
, is active in the generated code. By changing the value ofVar
, you can change the size ofA
and the active array.