MATLAB: How to typecast a variable to a custom Typedef in generated code

simulink coder

I have created a custom Type "MyTypeDef".
I have a Simulink model with a MATLAB function block which contains the following line:
coder.ceval('TestFunc',cast(u,'like',dt));
Where u is an input variable named "TestDefine" and dt is of type "MyTypeDef".
I am trying to cast TestDefine of type double to MyTypeDef.
The generated code is
TestFunc(TestDefine);
However I am expecting to see:
TestFunc((MyTypeDef)TestDefine);
Is the above code expected behavior? Is there an option to set to generate the code in the way I am expecting?

Best Answer

The behavior you are experiencing is expected. The coder identifies that the underlying base types for u and dt are the same and thus no typecast is necessary. You can "force" the typecast by changing either u (TestDefine) or dt to another base type, for example single; this would result in a typecast being shown:
Another option is for the customer to treat his "MyTypeDef" type as an opaque type and declare it as so. Then the MATLAB code would look like:
Which would result in the desired code:
However, note that the type is no longer determined by dt. You would probably have to use this in combination with a switch-case statement to insert the appropriate type based on dt.