Use Symbolic Dimensions for Signal Dimensions
Symbolic dimensions serve as placeholders representing signal dimensions in your Simulink® model. Instead of specifying fixed numbers for dimensions when building a model, you use symbols. By using symbols, you can experiment with different sizes or configurations without having to alter your model each time. Wherever you need to specify a signal dimension (such as for blocks and data objects) in your model, you can use these symbolic placeholders instead of actual numbers, which provides flexibility for changing the dimensions. These symbols propagate throughout the model during the simulation, and are incorporated into the generated code. The code generated includes these symbols as preprocessor conditionals, making the code flexible and able to work with different dimensions without having to regenerate the code. For more information on signal dimensions, see Signal Dimensions.
Symbolic dimensions are useful in scenarios where the size of data might change depending on the type of system. For example, imagine you are developing a Simulink model for tire-pressure monitoring system (TPMS) that monitors tire pressure data from multiple tires. The number of tires can vary depending on the type of vehicle (e.g., cars, trucks, motorcycles). By using symbolic dimensions to represent the number of tires, you can create a flexible model that adapts to different numbers of tires without altering the model structure.
Note
The symbolic dimension specification feature is on by default. To disable this feature, clear the Allow symbolic dimension specification parameter in the Configuration Parameters dialog box.
Create Symbolic Dimensions in Simulink Model
This example shows how to define a symbolic dimensions in a Simulink model that contains an Add block. You define the signal dimensions for the inport and outport block signals as symbolic in this example.
1. Use Simulink.Parameter
objects to specify dimension information as symbols. Define a symbolic dimension, S
, in the base workspace using Simulink.Parameter
.
S = Simulink.Parameter;
2. Set the initial value of the symbolic dimension, S
, to 3 and the data type as int32.
S.Value = 3;
S.DataType = 'int32';
3. Open the block parameters dialog box of the inport block In1
in the model. Replace the existing value of Port dimensions with the symbolic dimension S
.
4. Similarly, change the Port dimensions of In2
and Out1
to the symbolic dimension S
.
5. In the Debug tab, on the Information Overlays in Diagnostics menu, set the Signal Dimensions parameter.
6. Simulate the model.
Simulink® symbolically propagates the dimensions throughout the diagram during simulation. You can modify the dimension value of S
to a different size and simulate the model again to see the corresponding dimensions.
Generate Code
1.Open the Embedded Coder app.
2. In the Code Mappings editor, under the Parameters tab, set the storage class of S
to CompilerFlag.
NOTE: To use a Simulink.Parameter
object for dimension specification, it must be defined in the base workspace and 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.
3. You must fix the symbolic dimension values at compile-time. Open the Configuration Parameters dialog box and navigate to Code Generation > Custom Code. Define the value of S
.
4. Build the model.
5. View the generated code in <model>.h
and <model>_types.h
files, which include data definitions and preprocessor conditionals that incorporate symbolic dimensions.
Symbolic Expressions
You can establish modeling constraints that involve dimensions in your model. For
instance, consider C=A+B
, where the symbol C
is the
sum of the sizes of A
and B
. Simulink interprets these expressions during simulation and checks for consistency with
these constraints based on the numerical values assigned to these symbols ( For example, the
size of A
is 2, B
is 3, and C
is 5
in the below example). Simulink provides a warning for any violations of these constraints. You can use
MATLAB® expressions to specify symbolic dimension constraints.
When you generate code using symbolic expressions, they are encapsulated in preprocessor conditionals. These conditionals check the sizes or dimensions when the code is compiled to ensure everything fits together correctly. You must fix the symbolic dimension values at compile-time. You cannot resolve a symbolic dimension to another variable and then vary it dynamically during run-time. The dimension of a signal must remain constant while a simulation is executing.