Finely Control Data Representation by Writing TLC Code for a Storage Class
If creating your own storage class by using the properties in an Embedded Coder Dictionary or the Custom Storage Class Designer does not meet your requirements for controlling data representation in the generated code, you can write TLC code that explicitly controls the effect that a storage class has on the code. For example, to create a storage class that yields arbitrarily nested structures, you must write TLC code. In the Custom Storage Class Designer, these advanced storage classes haveTypeset toOther
. You cannot create such a storage class in an Embedded Coder Dictionary, so you cannot use the storage class as a default code generation setting in the Code Mapping Editor.
For an example, seeGenerate Code That Dereferences Data from a Literal Memory Address. For general information about TLC code, seeWhy Use the Target Language Compiler?(金宝app仿真软件编码器)。
Create Custom Attributes Class for Storage Class
As described inAllow Users of Storage Class to Specify Property Value, instance-specific properties enable users of a storage class to control the effect that the storage class has on each data item. For example, the built-in storage classExportToFile
has several instance-specific properties such asHeader fileandDefinition file.
When you create a storage class withTypeset toOther
, to add your own instance-specific properties that are not built into the Custom Storage Class Designer, create acustom attributes classfor your package. A custom attributes class is a MATLAB®class that you create as a subclass ofSimulink.CustomStorageClassAttributes
. Each property that you add to your custom attributes class appears to the user of the storage class as an instance-specific property.
To create your custom attributes class, seeDefine Data Classes(Simulink).
Write TLC Code for Storage Class
To control the effect of your storage class, write TLC code that specifies the code to generate for each data item.
In your package folder (for example,
+myPackage
), create atlc
folder.Copy a TLC template such as
TEMPLATE_v1.tlc
from the foldermatlabroot/toolbox/rtw/targets/ecoder/csc_templates
(open) into yourtlc
folder.Write your TLC code, following the comments in the template file. The comments describe how to specify, for example, how the generated code declares, defines, and accesses (by value or by reference) each data item.
Create Storage Class by Using Custom Storage Class Designer
To create your storage class in your package, you open the Custom Storage Class Designer in an advanced mode.
At the command prompt, enter:
cscdesigner-advanced
Select your package and create a storage class.
For the storage class, setTypeto
Other
. In theOther Attributespane, specify the name of your TLC file and the name of your custom attributes class.Set the properties on theOther Attributespane.
Is grouped: Select this option if you intend to combine multiple data items into a single entity in the generated code. For example, the built-in storage classes
BitField
andStruct
are grouped because they can aggregate multiple data items into a single structure variable.TLC file name: Enter the name of your TLC file. The Custom Storage Class Designer assumes that the file exists in the package
tlc
folder, so specify only the name of the file, not the file path.CSC attributes class name: (optional) If you created a custom attributes class, enter the full name of the class, including the package name. For example, specify
myPackage.myCustomAttsClass
. For more information, seeCreate Custom Attributes Class for Storage Class.
On theGeneralandCommentspanes, specify values for the remaining properties.
Related Topics
- Target Language Compiler Basics(Simulink Coder)
- Generate Code That Dereferences Data from a Literal Memory Address
- Create and Apply a Storage Class
- Apply Built-In and Customized Storage Classes to Data Elements
- Create Storage Classes by Using the Custom Storage Class Designer
- Define Storage Classes, Memory Sections, and Function Templates for Software Architecture
- Data Object Information in model.rtw(Simulink Coder)