Main Content

Target Language Compiler Function Conventions

You can find examples using these functions in matlabroot/toolbox/simulink/blocks/tlc_c and matlabroot/toolbox/simulink/sfuntemplates/tlc_c. The corresponding MEX S-function source code is located in matlabroot/simulink/src or matlabroot/toolbox/simulink/simdemos/simfeatures/src. MATLAB® file S-functions and the MEX-file executables (for example, sfunction.mex*) are located in matlabroot/toolbox/simulink/blocks or matlabroot/toolbox/simulink/simdemos/simfeatures. Clicking one of the preceding folder names changes to that folder in MATLAB and shows the folder contents in the MATLAB Current Folder browser.

Common Function Arguments

Several functions take similar or identical arguments. To simplify the reference pages, some of these arguments are documented in detail here instead of in the reference pages.

ArgumentDescription

portIdx

Refers to an input or output port index, starting at 0. For example, the first input port of an S-function is 0.

ucv

User control variable. This is an advanced feature that overrides the lcv and sigIdx parameters. When used within an inlined S-function, it should generally be specified as "".

lcv

Loop control variable. This is generally generated by the %roll directive via the second %roll argument (e.g., lcv=RollThreshold) and should be passed directly to the library function. It contains either "", indicating that the current pass through the %roll is being inlined, or it is the name of a loop control variable such as "i", indicating that the current pass through the %roll is being placed in a loop. Outside the %roll directive, this is usually specified as "".

sigIdxoridx

Signal index. Sometimes referred to as the signal element index. When accessing specific elements of an input or output signal directly, the call to the various library routines should have ucv="", lcv="", and sigIdx equal to the desired integer signal index starting at 0. For complex signals, sigIdx can be an overloaded integer index specifying both whether the real or imaginary part is being accessed and which element. When you access these items inside a %roll, use the sigIdx generated by the %roll directive.

Most functions that take a sigIdx argument accept it in an overloaded form, where sigIdx can be

  • An integer, e.g., 3. If the referenced signal is complex, then this refers to the identifier for the complex container. If the referenced signal is not complex, then this refers to the identifier.

  • An id-num, usually of the form (see Overloading sigIdx)

    1. "%<tRealPart>%<idx>" (e.g., "re3"). The real part of the signal element. Usually "%<tRealPart>%<sigIdx>" when sigIdx is generated by the %roll directive.

    2. "%<tImagPart>%<idx>" (e.g., "im3"). The imaginary part of the signal element or "" if the signal is not complex. Usually "%<tImagPart>%<sigIdx>" when sigIdx is generated by the %roll directive.

Use the idx name when referring to a state or work vector.

Functions that accept the three arguments ucv, lcv, sigIdx (or idx) are called differently depending upon whether or not they are used within a %roll directive. If they are used within a %roll directive, ucv is generally specified as "" and, lcv and sigIdx are the same as those specified in the %roll directive. If they are not used within a %roll directive, ucv and lcv are generally specified as "", and sigIdx specifies the index to access.

paramIdx

Parameter index. Sometimes referred to as the parameter element index. The handling of this parameter is very similar to sigIdx above: it can be #, re#, or im#.

stateIdx

State index. Sometimes referred to as the state vector element index. It must evaluate to an integer where the first element starts at 0.

Overloading sigIdx

The signal index (sigIdx sometimes written as idx) can be overloaded when passed to most library functions. Suppose you are interested in element 3 of a signal, and ucv="", lcv="". The following table shows

  • Values of sigIdx

  • Whether the signal being referenced is complex

  • What the function that uses sigIdx returns

  • An example of a returned variable

  • Data type of the returned variable

Note that “container” in the following table refers to the object that encapsulates both the real and imaginary parts of the number, e.g., creal_T, defined in tmwtypes.h.

sigIdxComplexFunction ReturnsExampleData
Type
"re3"

Yes

Real part of element 3

u0[2].re

real_T

"im3"

Yes

Imaginary part of element 3

u0[2].im

real_T

"3"

Yes

Complex container of element 3

u0[2]

creal_T

3

Yes

Complex container of element 3

u0[2]

creal_T

"re3"

No

Element 3

u0[2]

real_T

"im3"

No

""

N/A

N/A

"3"

No

Element 3

u0[2]

real_T

3

No

Element 3

u0[2]

real_T

Now suppose the following:

  1. You are interested in element 3 of a signal.

  2. (ucv = "i" AND lcv == "") OR (ucv = "" AND lcv = "i").

The following table shows values of idx, whether the signal is complex, and what the function that uses idx returns.

sigIdxComplexFunction Returns
"re3"

Yes

Real part of element i

"im3"

Yes

Imaginary part of element i

"3"

Yes

Complex container of element i

3

Yes

Complex container of element i

"re3"

No

Element i

"im3"

No

""

"3"

No

Element i

3

No

Element i

Notes

  • The vector index is added only for wide signals.

  • If ucv is not an empty string (""), then ucv is used instead of sigIdx in the above examples and both lcv and sigIdx are ignored.

  • If ucv is empty but lcv is not empty, then the function returns "&y%<portIdx>[%<lcv>]" and sigIdx is ignored.

  • It is assumed that the roller has declared and initialized the variables accessed inside the roller. The variables accessed inside the roller should be specified using rollVars as the argument to the %roll directive.

Related Topics