Where can I find TLC file examples in order to learn more about TLC routines?
MATLAB: Where can I find TLC file examples in order to learn more about TLC routines
examplefilelearnsimulink codertlc
Related Solutions
1.) The simplest use for the CGT is to be used for code markup, in fact much of the surface level functionality is geared towards this. For example we provide four built in definitions for code mark up:
File Banner Section
(Optional) This section contains comments and tokens you use in generating a custom file banner.
Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom function banner.
Shared Utility Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom shared utility function banner.
File Trailer Section
(Optional) This section contains comments for use in generating a custom trailer banner.
An example syntax would be something like this:
<FunctionBanner style = "open_box" width = "80"> Model Name : %<ModelName>
FunctionName : %<FunctionName>
Return Type : %<ReturnType>
Arguments : %<Arguments>
Block : %<GeneratedFor>
Abstract : %<FunctionDescription>
Block Description : %<BlockDescription style = "content_only">
</FunctionBanner>
Which would subsequently generate this in the code:
More in-depth syntax for these sections is described on this page here:
And the example that this was drawn from can be opened using
>> rtwdemo_codetemplate
2.) You can also use the CGT file to simply drop text you would like to have placed in the generated code before or after a desired section. For example if I modify this section of the CGT from this:
%<Includes>
%<Defines>
%<Types>
%<Enums>
%<Definitions>
%<Declarations>
%<Functions>
To this:
#include topinclude.h %<Includes> #include bottominclude.h %<Defines> %<Types> %<Enums> %<Definitions> %<Declarations> %<Functions>
then the subsequent code generation will produce the following:
This covers the basic functionality of the CGT file.
3.) Now, for more advanced use and specification of custom tokens, sections and subsections we need to bring in the Custom File Processing (CFP) template file described here:
The CFP file can be used for a few purposes, the most important of which are, generating your own additional source and header files, and modifying the predefined source and header files that normally get generated. The following code in a CFP file can be used to modify the standard generated header file, and serves as a good template for how to modify the predefined sections in the CGT file:
%%Add a #define to the model's public header file model.h
%assign pubName = LibGetMdlPubHdrBaseName()
%assign modelH = LibCreateSourceFile("Header", "Simulink", pubName)
%openfile tmpBuf
#define ACCELERATION 9.81 %closefile tmpBuf
%<LibSetSourceFileSection(modelH,"Defines",tmpBuf)>
The %assign statements are getting the name of the generated header file and grabbing the file itself
The text inside the %openfile %closefile pair is what we want to append to the header file
And the final statement is pushing the text from the buffer tmpBuf and appending it to modelH (the header file we specifed earlier) at the "Defines" section.
The entirety of the CFP doc page is a very good start if you want to dive deeper into TLC code generation and manipulating the Code Generation Output and I would suggest following through the example if you want to understand more about the creation of custom sections to use in the CGT file, or other customization of the code generation process. I would also just like to mention that if you do make custom modifications to the code generation process, we cannot guarantee the functionality of the newly modified code, so always make sure to check the effects of modifying these files.
Dear Glen,
It seems Mathworks has changed the API access for CGType massively since R2017b. The new API to query the CGType.Member.Name is LibCGTypeMemberName(cgTypeIdx, memberIdx) found in the following file.
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc:849:%% Function: LibCGTypeMemberName ===========================================
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-850-%% Abstract:
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-851-%% Returns the name of a member of a struct CGType
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-852-%%
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc:853:%function LibCGTypeMemberName(cgTypeIdx, memberIdx) void
I am not sure if you are still looking for a solution to this question. But, I believe it is one of the only questions relevant to the recent change in the CGType APIs that I found on the Google searches and yet unanswered.
I would suggest finding the rest of the changed APIs by grep-ing through the installed TLC files in R2018a.
Hope this helps!
Siraj
Best Answer