How do I convert between enumeration and integer types for use with HDL Coder?

I am using enumerations in my model, and would like to generate HDL code. However, when I use a data type conversion block to convert between integer types and enumerations and try to generate HDL code, I get an error:
ERROR: "Illegal conversion to/from an enumerated type in modelName/Data Type Conversion"
What is the recommended approach for converting between enums and integer types for HDL Coder?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2026 年 8 月 17 日 0:00
編集済み: MathWorks Support Team 2026 年 8 月 17 日 9:14

0 投票

The support for converting between enumerated and integer data types in HDL Coder depends on the release being used.
For Releases Prior to MATLAB R2021a:
In releases before MATLAB R2021a, HDL Coder does not support enumeration-to-integer or integer-to-enumeration conversion using the "Data Type Conversion block".
As a workaround, you may use the following:
1) Enumeration to integer: Use a "multiport switch" or use a MATLAB Function block with a "switch" statement, as shown in the attached model
2) Integer to enumeration: Use a "selector block" or use a MATLAB Function block with a "switch" statement, as shown in the attached model.
Please note that there is not one accepted approach, and each approach will generate slightly different HDL code. Therefore, please choose the option that best suits your modeling needs in Simulink.
Finally, please note that enumerations have further limitations when used with HDL Coder, as described here: https://www.mathworks.com/help/hdlcoder/ug/supported-data-types-and-matlab-operators-and-control-flow-statements.html
For MATLAB R2021a and Later:
Starting with MATLAB R2021a, HDL code generation supports enumerated signals at the ports of the "Data Type Conversion" block.
You can use the block directly to perform:
  • Enumeration → Integer conversion
  • Integer → Enumeration conversion
Specify the enumerated data type in the "Data Type Conversion block" and generate HDL code. An updated HDL-compatible example is attached for reference.

その他の回答 (1 件)

Kiran Kintali
Kiran Kintali 2021 年 7 月 9 日

1 投票

Updated Answer (R2021a)
HDL Code Generation for Data Type Conversion block supports enumerated data types in R2021a release. See web(fullfile(docroot, 'hdlcoder/release-notes.html'))
You can now use enumerated signals at the ports of the Data Type Conversion block for HDL code generation. You can use the Data Type Conversion block to convert the enumeration data to integer or integer-to-enumeration data. Specify your enumerated data to your Data Type Conversion block.
See attached updated Example showing HDL friendly model.

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by