Write Fully Inlined S-Functions - MATLAB & Simulink (original) (raw)

Main Content

A fully inlined S-function builds your algorithm (block) into generated code that you cannot distinguish from a built-in block. Typically, a fully inlined S-function requires you to implement your algorithm twice: once for the Simulink model (C/C++ MEX S-function) and once for code generation (TLC file).

Using the example in Write Wrapper S-Function and TLC Files, you can eliminate the call to my_alg entirely by specifying the explicit code (that is,2.0 * u) in wrapsfcn.tlc. While this can improve performance, if you are working with a large amount of C/C++ code, the task can be lengthy. You also have to maintain your algorithm in two places, the C/C++ S-function itself and the corresponding TLC file. Consider whether the performance gains might outweigh the disadvantages. To inline the algorithm used in this example, in theOutputs section of your wrapsfcn.tlc file, instead of writing:

Use:

This code is the code produced in mdlOutputs:

void mdlOutputs(int_T tid) { /* Sin Block: /Sin */ rtB.Sin = rtP.Sin.Amplitude * sin(rtP.Sin.Frequency * ssGetT(rtS) + rtP.Sin.Phase);

/* S-Function Block: /S-Function */  rtB.S_Function = 2.0 * rtB.Sin; /* Explicit embedding of algorithm */

/* Outport Block: /Out */ rtY.Out = rtB.S_Function; }

The Target Language Compiler replaces the call to my_alg with the algorithm itself.

Multiport S-Function

A more advanced multiport inlined S-function example is [sfun_multiport.c](https://mdsite.deno.dev/matlab:sfunddg%5Fcb%5Fedit%28'sfun%5Fmultiport'%29;) and [sfun_multiport.tlc](https://mdsite.deno.dev/matlab:edit%28fullfile%28matlabroot,'toolbox','simulink','sfuntemplates','tlc%5Fc','sfun%5Fmultiport.tlc'%29%29;). This S-function illustrates how to create a fully inlined TLC file for an S-function that contains multiple ports.

Guidelines for Writing Inlined S-Functions

To generate efficient code for S-Function blocks and to prevent unexpected behavior, adhere to these guidelines when writing inlined S-Functions.

Implementing the Block TLC Interface

Using RTWdata and mdlRTW

See Also

Topics