How to pass pointer arguments in Simulink for auto-generated code?

20 views (last 30 days)
The Simulink code generator generates call by reference functions but uses global variables. I need to make a reusable susbsystem which can accept different data store inputs similar to a call by reference implementation in C. How should I proceed?
Referenced subsystem block
Referenced subsystem call

Answers (1)

Rajanya
Rajanya on 3 Feb 2025 at 11:40
You can determine how the code should be generated for subsystem blocks by the 'Function Packaging' parameter, which is, by default, set to 'Auto' for Simulink to automatically determine the packaging.
Making the subsystem reference 'Atomic' (which tells Simulink to treat the block as a single unit while generating code) and changing the 'Function Packaging' parameter to 'Reusable' results in the generation of reusable functions with call by reference implementations.
The packaging can be set to 'Reusable' from Block Parameters of the subsystem > Check 'Treat as atomic unit' > Code Generation > Function Packaging > Reusable function.
The below sample model shows how to change the packaging and also the code generated for the model step function as a result -
The subsystem used in the reference -
Code generated -
Here, it can be seen that the 'exampleModel_SubsystemReference' can be reused and the data store variable is passed to the function via call by reference as the second parameter from the model step ('exampleModel_step').
You can refer to the following documentation to learn more about function packaging -https://www.mathworks.com/help/rtw/ug/reusing-functions-in-generated-code.html
Hope this answers your question! Thanks!

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!