How to generate conditionally executed code using "#if" preprocessor directives with Embedded Coder in MATLAB R2023b?

Some blocks in my model are conditionally executed depending on certain conditions, and I want the code generated by Embedded Coder to use "#if" preprocessor directives to achieve this behavior. An example is below:
#if MY_CONSTANT == 1
// Code here
#endif
I am using MATLAB R2023b on Windows 11 x64.
How can I do this?

 Risposta accettata

This functionality can be achieved using variant subsystems. Variant subsystems are executed depending on condition expressions involving variant control variables. Learn more about variant subsystems in the Variant Subsystem Documentation.
To achieve this behavior using "#if" preprocessor directives, set the "Variant activation time" parameter of the variant subsystem to "code compile".
However, if only model parameters depend on the conditions, you can use variant parameters instead. Similar to variant subsystems, these are assigned depending on condition expressions involving variant control variables.
To manage this behavior using “#if” preprocessor directives, you should set the “activation time” property of the variant control variable to “code compile”. Learn more about variant parameters in the Variant Parameters Documentation. A specific example of generating code with variant parameters and configuring preprocessor directives is outlined in the Generate C Code for Variant Parameters Section.

Più risposte (0)

Prodotti

Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by