| Real-Time Workshop® Embedded Coder™ | ![]() |
| On this page… |
|---|
An S-function wrapper is an S-function that calls your C or C++ code from within a Simulink model. S-function wrappers provide a standard interface between a Simulink model and externally written code, allowing you to integrate your code into a model with minimal modification. This is useful for software-in-the-loop (SIL) code verification (validating your generated code using Simulink software), as well as for simulation acceleration purposes. For a complete description of wrapper S-functions, see the Simulink Writing S-Functions document.
Using the Real-Time Workshop Embedded Coder Create Simulink (S-Function) block option, you can build, in one automated step:
A non-inlined C or C++ MEX S-function wrapper that calls Real-Time Workshop Embedded Coder generated code
A model containing the generated S-function block, ready for use with other blocks or models
When the Create Simulink (S-Function) block option is on, the Real-Time Workshop build process generates an additional source code file, model_sf.c or .cpp, in the build directory. This module contains the S-function that calls the Real-Time Workshop Embedded Coder code that you deploy. You can use this S-function in a Simulink model.
The build process then compiles and links model_sf.c or .cpp with model.c or .cpp and the other Embedded Coder generated code modules, building a MEX-file. The MEX-file is named model_sf.mexext. (mexext is the file extension for MEX-files on your platform, as given by the MATLAB mexext command.) The MEX-file is stored in your working directory. Finally, the Real-Time Workshop build process creates and opens an untitled model containing the generated S-Function block.
Note To generate a wrapper S-function for a subsystem, you can use a right-click subsystem build. Right-click the subsystem block in your model, select Real-Time Workshop > Generate S-Function, and in the Generate S-Function dialog box, select Use Embedded Coder and click Build. |
To generate an S-function wrapper for your Real-Time Workshop Embedded Coder code, open your ERT-based Simulink model and do the following:
Select the Create Simulink (S-Function) block option, as shown in this figure.

To ensure that memory for the S-Function is initialized to zero, you should deselect the following options in the Data Initialization subpane of the Optimization pane:
Select the Real-Time Workshop pane and click the Build button.
When the build process completes, an untitled model window opens. This model contains the generated S-Function block.

The generated S-Function block is now ready to use with other Simulink blocks or models.
The following limitations apply to Real-Time Workshop Embedded Coder S-function wrapper generation:
Continuous sample time is not supported. The Support continuous time option should not be selected when generating a Real-Time Workshop Embedded Coder S-function wrapper.
Models that contain S-function blocks for which the S-function is not inlined with a TLC file are not supported when generating a Real-Time Workshop Embedded Coder S-function wrapper.
You cannot use multiple instances of a generated Real-Time Workshop Embedded Coder S-function block within a model, because the code uses static memory allocation. Each instance potentially can overwrite global data values of the others.
Real-Time Workshop Embedded Coder S-function wrappers can be used with other blocks and models for such purposes as SIL code verification and simulation acceleration, but cannot be used for code generation.
A MEX S-function wrapper must only be used in the MATLAB version in which the wrapper is created.
![]() | Creating and Using a Code Generation Report | Exporting Function-Call Subsystems | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |