Adding Custom Code to specific Simulink Block

4 views (last 30 days)
I'm attempting to create real-time C code using the output of a Simulink Diagram. The only problem is that I have some existing code from a collection of external C/C++ files that I want to include at certain instances/locations in the C code generated from my Simulink Diagram. I am hoping to do this by creating a library of custom Simulink blocks that can each generate specified generic code at the appropriate place in the C code generated by the Simulink. It would be beneficial to have the additional functionality of having each block create specified code at Initialize and Finalize as well, but this isn't essential, as I could specify the code at the Initialize and Finalize level of the Diagram as a whole. I haven't been able to find any documentation on this, even on the custom block sections. Is this functionality possible?

Answers (1)

Sebastian Castro
Sebastian Castro on 27 Aug 2015
The documentation landing page I would refer to is this one:
Those 4 techniques are more or less ordered in terms of difficulty vs. flexibility.
There is an even simpler approach if you want to call the external C/C++ code only from the simulation time steps. To do this, you can use the coder.ceval function in a MATLAB Function block, which also gets generated in the code.
If you want control over the initialize/terminate behavior, I'd go either the Legacy Code Tool or S-Function route. Legacy Code Tool lets you bring in existing C/C++ functions, whereas S-Functions are manually authored wrappers around your C/C++ code. Again, more difficult but more flexibility.
In summary, you have a lot of options. As a general rule, you want to choose the easiest one that has just enough functionality to meet your needs.
- Sebastian

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!