Writing S-Functions    

Fully Inlined S-Function with the mdlRTW Routine

You can make a more fully inlined S-function that uses the S-function mdlRTW routine. The purpose of the mdlRTW routine is to provide the code generation process with more information about how the S-function is to be inlined, including:

mdlRTW does this by placing information into the model.rtw file. The mdlRTW routine is described in the text file matlabroot/simulink/src/sfuntmpl_doc.c.

As an example of how to use the mdlRTW function, this section discusses the steps you must take to create a direct-index lookup S-function. Lookup tables are collections of ordered data points of a function. Typically, these tables use some interpolation scheme to approximate values of the associated function between known data points. To incorporate the example lookup table algorithm in Simulink, the first step is to write an S-function that executes the algorithm in mdlOutputs. To produce the most efficient C code, the next step is to create a corresponding TLC file to eliminate computational overhead and improve the performance of the lookup computations.

For your convenience, Simulink provides support for two general purpose lookup 1-D and 2-D algorithms. You can use these algorithms as they are or create a custom lookup table S-function to fit your requirements. This section demonstrates how to create a 1-D lookup S-function, sfun_directlook.c, and its corresponding inlined sfun_directlook.tlc file. (See the Real-Time Workshop User's Guide and the Target Language Compiler Reference Guide for more details on the Target Language Compiler.) This 1-D direct-index lookup table example demonstrates the following concepts that you need to know to create your own custom lookup tables:


  Multiport S-Function Example S-Function RTWdata