Writing S-Functions | ![]() ![]() |
Types of S-Functions
The implementation of S-functions changes based on your requirements. This chapter discusses the typical problems that you may face and how to create S-functions for applications that need to work with Simulink and the Real-Time Workshop. These are some (informally defined) common situations:
"I want to create a block for my blockset that will be distributed throughout my organization. I'd like it to be very maintainable with efficient code. I'd like my algorithm to exist in one place but work with both Simulink and the Real-Time Workshop."
The MathWorks has adopted terminology for these different requirements. Respectively, the situations described above map to this terminology:
Noninlined S-Functions
A noninlined S-function is a C-MEX S-function that is treated identically by Simulink and the Real-Time Workshop. In general, you implement your algorithm once according to the S-function API. Simulink and the Real-Time Workshop call the S-function routines (e.g., mdlOutputs
) at the appropriate points during model execution.
Significant memory and computation resources are required for each instance of a noninlined S-Function block. However, this routine of incorporating algorithms into Simulink and the Real-Time Workshop is typical during the prototyping phase of a project where efficiency is not important. The advantage gained by forgoing efficiency is the ability to change model parameters and/or structures rapidly.
Note that writing a noninlined S-function does not involve any TLC coding. Noninlined S-functions are the default case for the Real-Time Workshop in the sense that once you've built a C-MEX S-function in your model, there is no additional preparation prior to clicking Build in the Real-Time Workshop Page of the Simulation Parameters dialog box for your model.
Wrapper S-Functions
A wrapper S-function is ideal for interfacing hand-written code or a large algorithm that is encapsulated within a few procedures. In this situation, usually the procedures reside in modules that are separate from the C-MEX S-function. The S-function module typically contains a few calls to your procedures. Because the S-function module does not contain any parts of your algorithm, but only calls your code, it is referred to as a wrapper S-function.
In addition to the C-MEX S-function wrapper, you need to create a TLC wrapper that complements your S-function. The TLC wrapper is similar to the S-function wrapper in that it contains calls to your algorithm.
Fully Inlined S-Functions
A fully inlined S-function builds your algorithm (block) into Simulink and the Real-Time Workshop in a manner that is indistinguishable from a built-in block. Typically, a fully inlined S-function requires you to implement your algorithm twice: once for Simulink (C-MEX S-function) and once for the Real-Time Workshop (TLC file). The complexity of the TLC file depends on the complexity of your algorithm and the level of efficiency you're trying to achieve in the generated code. TLC files vary from simple to complex in structure.
![]() | Classes of Problems Solved by S-Functions | Basic Files Required for Implementation | ![]() |