Target Language Compiler Function Conventions - MATLAB & Simulink (original) (raw)
Main Content
You can find examples using these functions in [_matlabroot_/toolbox/simulink/blocks/tlc_c](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/toolbox/simulink/blocks/tlc%5Fc']%29;)
and [_matlabroot_/toolbox/simulink/sfuntemplates/tlc_c](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/toolbox/simulink/sfuntemplates/tlc%5Fc']%29;)
. The corresponding MEX S-function source code is located in [_matlabroot_/simulink/src](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/simulink/src']%29;)
or [_matlabroot_/toolbox/simulink/simdemos/simfeatures/src](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/toolbox/simulink/simdemos/simfeatures/src']%29;)
. MATLAB® file S-functions and the MEX-file executables (for example,_`sfunction`_.mex*
) are located in[_matlabroot_/toolbox/simulink/blocks](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/toolbox/simulink/blocks']%29;)
or[_matlabroot_/toolbox/simulink/simdemos/simfeatures](https://mdsite.deno.dev/matlab:cd%28[matlabroot%20'/toolbox/simulink/simdemos/simfeatures']%29;)
.Clicking one of the preceding folder names changes to that folder in MATLAB Files panel and shows the folder contents in the MATLAB Files panel.
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.
Argument | Description |
---|---|
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 thelcv 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"". |
sigIdx_or_idx | 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 asigIdx argument accept it in an overloaded form, wheresigIdx can beAn 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)"%%" (e.g.,"re3"). The real part of the signal element. Usually"%%" whensigIdx is generated by the %roll directive."%%" (e.g.,"im3"). The imaginary part of the signal element or"" if the signal is not complex. Usually"%%" whensigIdx is generated by the %roll directive.Use the idx name when referring to a state or work vector.Functions that accept the three argumentsucv, lcv, sigIdx (oridx) 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 andsigIdx are the same as those specified in the %roll directive. If they are not used within a %roll directive, ucv andlcv are generally specified as "", andsigIdx 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#, orim#. |
stateIdx | State index. Sometimes referred to as the state vector element index. It must evaluate to an integer where the first element starts at0. |
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](https://mdsite.deno.dev/matlab:edit%28fullfile%28matlabroot,'extern',%20'include',%20'tmwtypes.h'%29%29)
.
sigIdx | Complex | Function Returns | Example | Data 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:
- You are interested in element 3 of a signal.
- (
ucv = "i"
ANDlcv == ""
) OR (ucv = ""
ANDlcv = "i"
).
The following table shows values of idx
, whether the signal is complex, and what the function that uses idx
returns.
sigIdx | Complex | Function 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 (""
), thenucv
is used instead ofsigIdx
in the above examples and bothlcv
andsigIdx
are ignored. - If
ucv
is empty butlcv
is not empty, then the function returns"&y%<portIdx>[%<lcv>]"
andsigIdx
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.