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 views (last 30 days)
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?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Aug 2012
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.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2012a

Community Treasure Hunt

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

Start Hunting!