MATLAB: How to generate a custom header file for an enumerated class used in Simulink model by Simulink Coder 8.4 (R2013a)

enumeratedgetheaderfilesimulink coder

I would like to generate a custom header file for an enumerated class. I am using the enumerated signal in my Simulink model and defined enumerated data type in MATLAB. When generating C code, the definition of enumerated data is in the model header.

Best Answer

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)
end
methods (Static = true)
function retVal = getDefaultValue()
retVal = BasicColors.Blue;
end
function retVal = getDataScope()
retVal = 'Exported';
end
function retVal = getHeaderFile()
retVal = 'BasicColors.h';
end
end
end