Execution order of blcks

How can I control the execution order of blocks so that certain computations are completed before the Outport updates, especially when the generated code doesn’t follow the visual or manual block order set in the model?

 Accepted Answer

To guarantee that the Outport executes after all filter calculation in code generation, you should take extra steps beyond setting “First”/”Last” execution order because code generation optimization may otherwise reorder blocks. You can follow the steps to ensure correct execution order:
1. Group your blocks into a subsystem and place the Outport block in its own subsystem.
2. For each subsystem:
  1. Open Block Parameter -> Code Generation tab
  2. Set Function packaging to Nonreusable function.
  3. To prevent code generation optimizations reordering these operations, mark the subsystem for late inling using the following MATLAB command:
set_param(model_name/subsystem_name, 'RTWSystemCode', 'Inline');
3. In the Simulink model, ensure that your blocks execute before Outport subsystem. You can do this by :
  1. Adjust your block priorities.
  2. Using Model Configuration Parameter -> Simulink Order
Hope this helps!

Categories

Asked:

on 5 May 2025

Answered:

on 9 May 2025

Community Treasure Hunt

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

Start Hunting!