MATLAB: 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)

matlab coder

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?

Best Answer

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.