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.
Only execution coverage is measured.
Simulink Code Inspector™
Simulink Design Verifier™
– Test harness
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
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 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. To see a list of the C Math Library functions that you can call directly, see Call C Library Functions From C Function Block. To call other C library functions, create a wrapper function that calls the C library function.
Input port— Input of the C Function block
Input to the C Function block.
The number of inputs and their names are determined by the definitions in your external C code and the definitions of the symbols in the Symbols table in the block parameters dialog.
The input port label is the same as the name of the input symbol unless you change it by editing the Label field in the Symbols table of the blocks dialog.
Output port— Output of the C Function block
Output from the C Function block.
Number of outputs and their names are determined by the definitions in your external C code and the definitions of the symbols in the Symbols table in the block parameters dialog.
The output port label is the same as the name of the output symbol unless you change it by editing the Label field in the Symbols table of the block parameters dialog.
Output Code— Output code
Specifies the code that the C Function block executes during simulation. For example, you can call a function from the external C code specified in the Simulation Target pane, make modifications to the results, and perform operations to pass the results to other blocks.
Start Code— Initialization code
Specifies the initialization code of the C Function block. This code executes one time at the start of simulation. For example, you can initialize persistent data.
Terminate Code— Termination code
Specifies cleanup code that the C Function block runs at model termination. This code executes once at the end of a simulation. For example, use this code to free the memory cached on persistent symbols specified as void pointers.
Symbols— Specify symbol properties
The Symbols table specifies the attributes of the symbols in the C code. You must enter the attributes of each symbol in the table.
Name — Symbol name in the source code.
Scope — Scope of the symbol. You can change the scope of a symbol at any time. The following scopes are available:
Input — Input symbol to the C Function
Output — Output symbol to the C
InputOutput — Define a symbol as both input and
output to the C Function block.
Parameter — Define a symbol as a parameter. The
parameter name is defined by the
Label property of the
Persistent — Define a symbol as persistent
You can define a void pointer using the
scope. A void pointer is a pointer that can store any
type of data that you create or allocate.
Constant — Define a symbol as constant using
value-size or numeric expressions.
Label — Label of the symbol. For symbols with
their scope set to
Output, this label appears as the port name on the
block. For symbols with their scope set to
this label is the label that appears on the block parameter mask. If the scope is
Constant, the label is the constant expression. You
cannot define a label for
Type — Data type of the symbol. Select a data type from the drop-down list or specify custom data type.
To use a custom type such as
Simulink.Bus, Simulink Enum or
Simulink.AliasType that does not have an external header
definition associated with a C Function block, set the type
accordingly on the Symbol table.
Size — Size of the symbol data. The C
Function block supports only scalars and vectors; matrices and
higher-dimension arrays are not supported. You can use a size expression to define
the size of an output or use
-1 to inherit size.
Port — For input and output symbols,
Port indicates the port index on the block of the symbol
data. For parameter symbols,
Port indicates the order that the
symbol appears in the block parameter mask.
Actual data type or capability support depends on block implementation.