Target Language Compiler | ![]() ![]() |
Records
One of the constructs most relevant to generating code from the model
.rtw
file is a record. A record is very similar to a structure in C or a record in Pascal. The syntax of a record declaration is
%createrecord recVar { ... field1 value1 ... field2 value2 ... ... fieldN valueN ... }
where recVar
is the name of the variable that references this record while recType
is the record itself. fieldi
is a string and valuei
is the corresponding Target Language Compiler value.
Records can have nested records, or subrecords, within them. The model
.rtw
file is essentially one large record, named CompiledModel
, containing several subrecords. Thus, a simple program that loops through a model and outputs the name of all blocks in the model would look like the following code.
%include "utillib.tlc" %selectfile STDOUT %with CompiledModel %foreach sysIdx = NumNonvirtSubsystems + 1 %assign ss = System[sysIdx] %with ss %foreach blkIdx = NumBlocks %assign block = Block[blkIdx] %<LibGetFormattedBlockPath(block)> %endforeach %endwith %endforeach %endwith
Unlike MATLAB, the Target Language Compiler requires that you explicitly load any function definitions not located in the same target file. In MATLAB, the line A = myfunc(B)
causes MATLAB to automatically search for and load an M-file or MEX-file named myfunc
. The Target Language Compiler, on the other hand, requires that you specifically include the file that defines the function. In this case, utillib.tlc
contains the definition of LibGetFormattedBlockPath
.
Like Pascal, the Target Language Compiler provides a %with
directive that facilitates using records. See Chapter 5, Directives and Built-in Functions, for a detailed description of the directive and its associated scoping rules.
Note
Appendix A, model.rtw, describes in detail the structure of the model .rtw file including all the field names and the interpretation of their values.
|
A record read in from a file is not immutable. It is like any other record that you might declare in a program. In fact, the global CompiledModel
Real-Time Workshop record is modified many times during code generation. CompiledModel
is the global record in the model
.rtw
file. It contains all the variables necessary for code generation such as NumNonvirtSubsystems
, NumBlocks
, etc. It is also appended during code generation with many new variables, flags, and subrecords as needed.
Functions such as LibGetFormattedBlockPath
are provided in the Target Language Compiler libraries located in matlabroot
/rtw/c/tlc/*.tlc
. For a complete list of available functions, refer to Chapter 9, TLC Function Library Reference.
Assigning Values to Fields of Records
To assign a value to a field of a record you must use a qualified variable expression.
A qualified variable expressions references a variable in one of the following forms:
var[2].b
var[expr]
![]() | Variable Types | To assign a value to a field of a record you must use a qualified variable expression. | ![]() |