Hello, I am working on model based application development using Matlab Simulink. I have defined an enum in a script(.m file) in matlab. I am using the elements of the enum in a model as enumerated constants. Now, when I generate C code out of the model, I want the enum to be generated as a seperate header file(.h) with a particular name say abcd.h Does anybody know how to implement this? Thanks in advance.
MATLAB: How to obtain a header file with customized name on code generation from an enum defined in a script(.m) file in matlab
code generationsimulink
Related Solutions
Every enumerated class has four associated static methods, which it inherits from Simulink.IntEnumType. You can optionally override these static methods to customize the behavior of an enumerated type. The methods are:
getDefaultValue — Returns the default value of the enumerated data type.
getDescription — Returns a description of the enumerated data type.
— Specifies a file where the type is defined for generated code.
addClassNameToEnumNames — Specifies whether the class name becomes a prefix in code.
Ues "getHeaderFile" to prevent the declaration of an enumerated type from being embedded in the generated code, allowing you to provide the declaration in an external file, include the following method in the enumerated class's methods section:
function retVal = getHeaderFile() retVal = 'filename';end
Following codes show how to define a Enumerated class that generate a header file called "BasicColors.h" after code generation:
classdef BasicColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Blue(2) endmethods (Static = true) function retVal = getDefaultValue() retVal = BasicColors.Blue; end function retVal = getDataScope() retVal = 'Exported'; end function retVal = getHeaderFile() retVal = 'BasicColors.h'; end endend
Root-cause of the issue:
- The error in compilation is referring to an internal data type whose definition is not being generated in “Rte_type.h” (we define data types as internal when they are not used on any interfaces with the RTE).
- It is not recommended for internal types to have “Rte_type.h” set as header file, because in the generated code, none of the RTE calls need to use the internal types (by definition).
- Instead, having them in a header file corresponding locally to the specific SWC would provide a cleaner partition of the code.
The recommended workaround:
- I see that you are defining the enumerated data types with Simulink.defineIntEnumType:
- Currently, I see that the “HeaderFile” is set to “Rte_type.h”, but no setting for “DataScope” is provided when defining your enum types.
- By default, according to the documentation, when having a HeaderFile but not a DataScope property, Simulink is expecting the type to be imported from “Rte_type.h”, and hence the error message.
Depending on your use case, there are two recommended approaches:
1. Top-down workflow:
- In this case, you can either:
- add a “HeaderFile” property with a header file that you choose, and “DataScope” set to “Exported” (this way this internal type’s definition will be generated in that specific header file), or
- simply do no set any values for “HeaderFile” and “DataScope” properties. In this case, the type will be exported to “modelname_types.h”.
As an example of the two options using the enumerated data type that you showed above:
1. Selecting a HeaderFile and DataScope set to “Exported” (data type definition will be generated in “myheaderfile.h”):
Simulink.defineIntEnumType( 'myEnumType', ... {'a','b','c'}, ... [0 1 2], ... 'DefaultValue', 'a', ... 'StorageType', 'uint8', ... 'HeaderFile', 'myheaderfile.h', ... 'DataScope', 'Exported', ... 'AddClassNameToEnumNames', true);
2. Not adding any HeaderFile or DataScope (data type definition by default generated in “modelname_types.h”):
Simulink.defineIntEnumType( 'myEnumType', ... {'a','b','c'}, ... [0 1 2], ... 'DefaultValue', 'a', ... 'StorageType', 'uint8', ... 'AddClassNameToEnumNames', true);
2. Bottom-up workflow: (from MATLAB R2020b release)
- If you are using Simulink as an AUTOSAR authoring tool and would require all data types to be defined in the generated code, you can use this capability from MATLAB R2020b release on.
- With this new feature, you can export definitions of internal types, and for your particular use case, you can select the internal types to be exported to "Rte_type.h".
- For this, you can choose "Rte_type.h" as HeaderFile, and set the DataScope property to "Exported", as follows:
Simulink.defineIntEnumType( 'myEnumType', ... {'a','b','c'}, ... [0 1 2], ... 'DefaultValue', 'a', ... 'StorageType', 'uint8', ... 'HeaderFile', 'Rte_type.h', ... 'DataScope', 'Exported', ... 'AddClassNameToEnumNames', true);
This would make the definition for "myEnumType" to be generated in "Rte_type.h".
Best Answer