Clear Filters
Clear Filters

Conditional Compilation for code generated from Simulink Model

29 views (last 30 days)
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.

Accepted Answer

Ayush Singh
Ayush Singh on 18 Jul 2024 at 11:20
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 Comments
wenchao zhang
wenchao zhang on 18 Jul 2024 at 22:20
hi Ayush,thanks for your answer,i will try these suggestion.
wenchao zhang
wenchao zhang on 18 Jul 2024 at 22:41
Edited: wenchao zhang on 18 Jul 2024 at 22:42
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.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!