MATLAB Answers

How to replace the Simulink data types with user-defined data types in the generated code?

17 views (last 30 days)
How to replace the Simulink data types with user-defined data types in the generated code?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Mar 2021
In the configuration parameters, under Hardware Implementation, the Device details section lists the data type and the number of bits for each data type for the selected hardware. In case, if the Test hardware is different from the Production hardware, the test hardware shall be selected from the "Hardware Implementation-> Advanced parameters". 
When the code is generated using embedded coder, rtwtypes.h header file is generated automatically based on the selected Production hardware or Test hardware (in case, the Test hardware is different from the Production hardware) and the data types in the auto-generated rtwtypes.h file cannot be changed manually. However, if you want to replace the Simulink data types with the user-defined data types there is a provision to do so by defining the user-defined data types in the custom header file and import and use these user-defined data types in the generated code. These user-defined data types will replace the Simulink data types directly in the generated code.  
Perform the below steps to define the user-defined data types and use them in the generated code. For example, if you want to replace the long long type in the model by 'Myint64Type' type by overriding the default type of "int64_T":  
1. Add a custom header file, say 'my_types.h' under the same path as the Simulink model , and define the below user-defined data types 'Myint64Type' and 'MyUint64Type':  
typedef long long Myint64Type;
typedef unsigned long long MyUint64Type;
2. Import the user-defined data types to the model by adding the below PreLoadFcn callback function in the model properties:  
Simulink.importExternalCTypes('my_types.h');
3. In the 'Data Type Replacement' pane under 'Code Generation', enable the 'Replace data type names in the generated code' option and add the replacements MyUint64Type for uint64 and Myint64Type for int64.
After generating the code with the above changes, the below changes can be noticed in the generated code:  
1. The custom header file 'my_types.h' will be included at the end of rtwtypes.h file.  
2. The data types int64_T and uint64_T in the previously generated code will now be replaced with Myint64Type and MyUint64Type respectively which are defined as 'long long' and 'unsigned long long' in my_types.h file.  
3. There is no change to the data types int64_T and uint64_T defined in rtwtypes.h file. However, we are replacing them with user-defined data types in the generated code.  

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!