Integrate and call external C code from a Simulink model
Simulink / User-Defined Functions
The C Function block integrates and calls external C code from a Simulink® model. Use this block to define external code and customize the integration of your code by preprocessing or postprocessing the data. In addition, you can specify customized code for simulation and C code generation. You can call functions defined in your code conditionally, and you can also call multiple functions in one block. Using this block, you can initialize the block's persistent data and pass it to an external function.
You can also use C Function block to call a subset of C Math Library functions. See Call C Library Functions From C Function Block for more information.
The C Function block supports initializing persistent data and calling external functions from the block dialog. The C Function block supports only initializing and terminating persistent data; the block does not support updating the data during simulation. To model a dynamic system with continuous states, use an S-Function block. To learn more about S-functions, see What Is an S-Function?.
Define the source code and supporting files to be called by the C Function block in the Simulation Target pane of the configuration parameters.
The following features of Simulink are not compatible with the C Function block.
Simulink Coverage™
Only execution coverage is measured.
Simulink Code Inspector™
Simulink Design Verifier™
– Test harness
Use of the C Function block in Simulink Report Generator™
In addition, the following limitations apply to the source code referenced by a C Function block.
The C keyword static
is not supported.
To cache values across time steps, define the symbol as
Persistent
in the Symbols table of the
block dialog.
Files cannot be included in the script.
External functions must be specified in the Simulation Target pane of the Configuration Parameters dialog.
If you have existing includes defined in the Simulation Target pane that cannot be mixed with the new include, consider adding the include and C Function block in a library model and use a link to the block in the main model. For more information, see Create a Custom Library.
Pointers of different types cannot be assigned to each other. The pointer types should match when you call external functions.
The code cannot take the address of a constant.
Directly calling of some C library functions from the C Function block is not supported. See <link here> to see a list of the C Math Library functions that you can directly call. To call other C library functions, create a wrapper function that calls the C library function.