User`s guide

4 Guidelines for Writing C MEX S-Functions
4-20
Fully Inlined S-Function with the mdlRTW Routine
You can make a more fully inlined S-function that uses t he S-function mdlRTW
routine. The purpose of the mdlRTW routineistoprovidethecodegeneration
process with more information about how the S-function is to be inlined,
including:
Renaming of tunable parameters in the generated code. This improves
readability of the code by replacing
p1, p2, etc., by names of y our choice.
Creating a parameter record of a nontunable parameter for use with a TLC
file.
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.
As an example of how to use the
mdlRTW function, this section discusses the
steps y ou must take to create a direct-index lookup S-function. Look-up tables
are a collection of ordered data points of a function. Typically, these tables use
some interpolation scheme to approximate val ues of the associated function
between knowndatapoints.Toincorporatetheexamplelookuptablealgorithm
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
acorrespond
ingTLC fileto eliminatecomputationaloverheadandimprovethe
performance of the lookup computations.
For y our convenience, Simulink provides support for two general purpose
lookup 1-D and 2-D algorithms. You can use these algorithms a s they are or
create a custom lookup table S-function to fit your requirements. This section
demonstrates how t o create a 1-D lookup S-function (
sfun_directlook.c)and
its corresponding inlined
sfun_directlook.tlc file (see the Real-Time
WorkshopUser’s Guide and the Target LanguageCompiler Reference Guide for
more detail s 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:
Error checking of S-function parameters
Caching of information for the S-function that doesn’t change during model
execution