Writing Fully Inlined S-Functions
Continuing the example of the previous section, you could eliminate
the call to my_alg entirely by specifying the
explicit code (that is, 2.0 * u) in wrapsfcn.tlc.
This is referred to as a fully inlined S-function.
While this can improve performance, if you are working with a large
amount of C/C++ code, this can be a lengthy task. In addition, you
now have to maintain your algorithm in two places, the C/C++ S-function
itself and the corresponding TLC file. However, the performance gains
might outweigh the disadvantages. To inline the algorithm used in
this example, in the Outputs section of your wrapsfcn.tlc file,
instead of writing
%<y> = my_alg(%<u>);
use
%<y> = 2.0 * %<u>;
This is the code produced in mdlOutputs:
void mdlOutputs(int_T tid)
{
/* Sin Block: <Root>/Sin */
rtB.Sin = rtP.Sin.Amplitude *
sin(rtP.Sin.Frequency * ssGetT(rtS) + rtP.Sin.Phase);
/* S-Function Block: <Root>/S-Function */
rtB.S_Function = 2.0 * rtB.Sin; /* Explicit embedding of algorithm */
/* Outport Block: <Root>/Out */
rtY.Out = rtB.S_Function;
}
The Target Language Compiler has replaced the call to my_alg with
the algorithm itself.
Multiport S-Function Example
A more advanced multiport inlined S-function example is sfun_multiport.c and
matlabrootsfun_multiport.tlc.
This S-function demonstrates how to create a fully inlined TLC file
for an S-function that contains multiple ports. You might find that
looking at this example helps you to understand fully inlined TLC
files.
Back to Top
 | Writing Wrapper S-Functions | | Automating the Generation of Files for Fully Inlined S-Functions
Using Legacy Code Tool |  |
Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
Get this Simulink Kit