Why do I get an error generating code from a MATLAB class that can conditionally assign multiple data types to one of its properties in MATLAB Coder 2.2 (R2012a)?

3 ビュー (過去 30 日間)
I have created a MATLAB Class called 'sampleClass' which contains a property called 'sampleProperty'. The class is initialized with a single argument 'caseNum' as follows:
function obj = sampleClass(caseNum)
if caseNum == 1
obj.sampleProperty = uint8(1);
else
obj.sampleProperty = uint16(1);
end
end
In this way, the property is assigned to a uint8 data type if 'caseNum' is 1, and a uint16 data type otherwise. This works without any issues in MATLAB, but when I use MATLAB Coder 2.2 (R2012a) to generate code using this class, I get the following error:
??? Class mismatch (uint8 ~= uint16).
The class to the left is the class of the left-hand side of the assignment.
Is there any way I can work around this?

採用された回答

MathWorks Support Team
MathWorks Support Team 2012 年 8 月 21 日
The ability to assign a different data type to the property of a MATLAB Class based on some conditional statement is not supported for code generation using MATLAB Coder 2.2 (R2012a).
As a workaround, you can create two different properties of different data types and include the appropriate logic in your code to use the correct property based on this conditional statement.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by