As described in Set Variable Value by Using a Mathematical Expression, you can set the value of
a Simulink.Parameter
object to an expression involving other MATLAB® variables or parameter objects. When you generate code from a model that uses
such a parameter object, to achieve your goal, use the information in the table.
Goal | Technique and More Information |
---|---|
Generate code without Embedded Coder®. | For the parameter object that uses the expression, apply an available storage class (see C Code Generation Configuration for Model Interface Elements). For the parameter objects used in
the expression, apply only the storage class
|
Generate code that defines a global variable and initializes it by using an expression involving system constants and other macros (requires Embedded Coder). | See Initialize Parameter Value From System Constant or Other Macro (Embedded Coder) (Embedded Coder). |
Generate code that defines a macro whose value is an expression involving other macros (requires Embedded Coder). | For the parameter object that uses the expression, apply a storage class
that yields a macro in the generated code, such as For the parameter objects used in
the expression, apply storage classes that yield imported macros. For example, use
the storage class |
To avoid errors that prevent code generation, if you apply a storage class other
than Auto
to the dependent parameter object (which uses the
expression as its value), the parameter objects used in the expression must use either
Auto
or a storage class that yields a macro in the generated
code.
You cannot set the value of a parameter object that represents a symbolic dimension (see Implement Dimension Variants for Array Sizes in Generated Code (Embedded Coder)) to an expression.
With Embedded Coder, you can generate code that preserves the expression, but only for the purpose of statically initializing the value of a global variable or macro that corresponds to the dependent parameter object. Follow the guidelines in Expression Preservation.
If you want Embedded Coder to preserve expressions in the generated code, adhere to these restrictions and guidelines.
The dependent parameter object must use a storage class other than
Auto
. For example, to generate a global variable and initialize
it by using the expression, use ExportedGlobal
or
ExportToFile
.
The parameter objects used in the expression must:
Use a storage class that yields a macro in the generated code, such as
Define
.
Have scalar, real values if the expression uses operators.
The expression can use only these operators (in MATLAB syntax):
Mathematical: +
, -
,
*
Relational: ==
, ~=
,
<
, >
, <=
,
>=
These data typing guidelines apply:
If possible, for a parameter object used in an expression, leave the
DataType
property at the default value,
auto
.
To use a value other than auto
, you must set the
DataType
property of the object that uses the expression
to the same value. Otherwise, the code generator does not preserve the
expression.
If you want to use a parameter object in multiple different expressions, and
the dependent parameter objects have different data types, leave the
DataType
property of the independent object at the
default value, auto
. In the generated code, the value of the
macro is expressed as a floating-point number (with effective data type
double
), and the code initializes the dependent parameters
by typecasting the result of each expression.