Conditional Compilation for code generated from Simulink Model

8 次查看(过去 30 天)
hi, as following picture shows, when after generate code, during compile how to control just let fun2 code enter compile process, fun1 code is forbidden to enter compile.

采纳的回答

Ayush Singh
Ayush Singh 2024-7-18
Hey wenchao,
To control the inclusion of specific blocks during the code generation process in Simulink, you can use conditional subsystems or model variants. These techniques allow you to specify conditions under which certain parts of your model are included or excluded during code generation.
Here, the focus is on using a conditional subsystem to include 'fun2' and exclude 'fun1' during the code generation process.
1. Add a control signal:
- Create a constant block (Simulink > Sources > Constant).
- Set the value of the constant block to `1` to enable the 'fun2' subsystem during code generation.
2. Disable the subsystem 'fun1'
- Double-click the 'fun1' subsystem.
- Right-click on the subsystem block and select Block Parameters (Subsystem).
- Check the Treat as atomic unit option.
- Click OK.
3. Configure the subsystem to be conditionally excluded:
- Right-click the 'fun1' subsystem and select Block Parameters (Subsystem).
- Set the Function packaging parameter to 'Nonreusable function'.
- Click OK.
4. Use a variant subsystem to control inclusion:
- Drag a 'Variant Subsystem' block from the Simulink Library Browser (Simulink > Ports & Subsystems > Variant Subsystem).
- Replace the 'fun1' with the 'Variant Subsystem'.
- Add Variants to the Variant Subsystem:
  • Double-click the 'Variant Subsystem' block to open it.
  • You will see two default subsystems inside: 'Variant 1' and 'Variant 2'.
  • Configure the Variants:
- Move ConditionalSubsystem_fun2 (the subsystem containing fun2) into the 'Variant 2'.
- Move DisabledSubsystem_fun1 (the subsystem containing fun1) into the 'Variant 1'.
- Set the conditions under which each variant is active. This is done by defining a variant control variable in MATLAB.
By using a Variant Subsystem and setting variant conditions, you can control which parts of your model are included during code generation. This approach allows you to include fun2 and exclude fun1 based on the value of the variant control. This method provides flexibility in managing complex models and ensures that only the desired components are compiled.
  2 个评论
wenchao zhang
wenchao zhang 2024-7-18
编辑:wenchao zhang 2024-7-18
hi,Ayush,yes it seems the first idea is not flexable, use variant subsystem,i can control which function join the final compile,it is convenient and more efficient.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Variant Inline Components 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by