C Function
Libraries:
Simulink /
User-Defined Functions
Description
The C Function block integrates and calls external C/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 conditionally call functions defined in your code, and you can also call multiple functions in one block. Using this block, you can initialize persistent data and pass it to an external function.
The C Function block supports initializing persistent data and calling external functions from the block dialog box. Persistent data can include an object of a C++ class defined in your custom code. See Interface with C++ Classes Using C Function Block. The 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 Model Configuration Parameters dialog box. See Model Configuration Parameters: Simulation Target.
Note
C99 is the standard version of C language supported for custom C code integration into Simulink.
Call C Library Functions
You can call these C Math Library functions directly from the C Function block.
abs | acos | asin | atan | atan2 | ceil |
cos | cosh | exp | fabs | floor | fmod |
labs | ldexp | log | log10 | pow | sin |
sinh | sqrt | tan | tanh |
When you call these functions, double precision applies unless all the input arguments
are explicitly single precision. When a type mismatch occurs, a cast of the input arguments
to the expected type replaces the original arguments. For example, if you call the
sin
function with an integer argument, a cast of the input argument to
a floating-point number of type double
replaces the original
argument.
To call other C library functions, create and call an external wrapper function that calls the C library function.
abs
, fabs
, and labs
FunctionsInterpretation of the abs
, fabs
, and
labs
functions in C Function block goes beyond the
standard C version to include integer and floating-point arguments:
If
x
is an integer, the standard C functionabs
applies tox
, orabs(x)
.If
x
is a double, the standard C functionlabs
applies tox
, orlabs(x)
.If
x
is a single, the standard C functionfabs
applies tox
, orfabs(x)
.
The call to the function should call the correct CRL based on the type of data passed into the function. If no CRL is specified, the call to the function should call to type-specific library. The CRL for C99 generates a type-specific function. For example:
Type passed in | Code generation call |
---|---|
sin(doubleIn) | sin(doubleIn) |
sin(floatIn) | sinf(floatIn) |
Examples
Limitations
These features of Simulink are not compatible with the C Function block.
Simulink Coverage™
Only execution coverage is measured.
Simulink Code Inspector™
Simulink Design Verifier™
These limitations apply to the C code that you specify in a C Function block.
Local static variables using the
static
keyword are not supported. To cache values across time steps, define a symbol asPersistent
in the Symbols table of the block dialog box.You cannot
#include
files in the code. Files containing external functions must be specified in the Simulation Target pane of the Configuration Parameters window.Taking the address of a
Constant
symbol is not supported.Directly calling C library functions other than the C Math Library functions listed above under Call C Library Functions is not supported. To call other C library functions, create and call a wrapper function that calls the C library function.
Ports
Input
Output
Parameters
Block Characteristics
Extended Capabilities
Version History
Introduced in R2020a