MATLAB: How to insert macro before and after the function corresponding to atomic subsystem in the generated code

Embedded Coder

I would like to insert macro before and after my custom function in the generated code. For example,
#define a 1
void test(void)
{...}
#define end 1
How do I implement this?

Best Answer

This can be done using memory section.
1. Create a class data package 'mypkg' using the following steps:
View the +SimulinkDemos data class package in the folder _matlabroot_/toolbox/simulink/simdemos/dataclasses. This package contains predefined data classes.
Copy the folder to the location where you want to define your data classes.
Rename the folder +mypkg and add its parent folder to the MATLAB path.
2. Run the following command to open the custom storage class designer:
>> cscdesigner('mypkg')
3. Under "Memory Section", create a new Definition, 'myMS'.
4. Selecting this, in the "Pre-memory-section pragma" field, enter "#define a 1" without the quotes. And in the "Post-memory-section pragma" field, enter "#define end 1" without the quotes.
5. Click OK and restart MATLAB to reflect the change.
6. In your model, go to Configuration Parameters -> Code Generation -> Memory Sections and then select the 'mypkg' package from "Package" drop-down list.
7. Right click the atomic subsystem and select "Block Parameters (Subsystem)", under "Code Generation" select 'myMS' from "Memory section for execution functions" drop-down list
8. Click OK and generate code for the model.
More information can be found in the following documentation page:
<https://www.mathworks.com/help/releases/R2017b/ecoder/ug/control-data-and-function-placement-in-memory-by-inserting-pragmas.html https://www.mathworks.com/help/ecoder/ug/control-data-and-function-placement-in-memory-by-inserting-pragmas.html>