MATLAB: Is it possible to perform casting for a non built-in data type in Stateflow with the function cast

castconversiondatarescalestateflowtype

I define a Simulink.NumericType object 'MY_TYPE' as follows:
>> MY_TYPE = Simulink.NumericType;
>> MY_TYPE.DataTypeMode = 'Fixed-point: slope and bias scaling';
>> MY_TYPE.Signedness = 'Unsigned';
>> MY_TYPE.WordLength = 32;
>> MY_TYPE.Slope = 1000;
>> MY_TYPE.Bias = 0;
>> MY_TYPE.IsAlias = 1;
>> MY_TYPE.DataScope = 'Auto';
>> MY_TYPE.HeaderFile = 'Rte_Type.h';
and then try to cast an input variable IN to such type in a Stateflow chart:
OUT = cast(IN,'MY_TYPE');
However, I encounter the following error:
'MY_TYPE' is not supported as an operand to cast
When instead I use a Data Type Conversion block in Simulink, the conversion to my custom type is correctly executed.
Is it possible to perform casting for a non built-in data type in Stateflow? Alternatively, how can I implement in Stateflow the corresponding behaviour of the Data Type Conversion block in Simulink?

Best Answer

To reproduce in Stateflow the output of the Simulink block 'Data Type Conversion', after defining your custom data type you need also to define a Simulink.Signal object with the same name, with the desired DataType:
>> OUT = Simulink.Signal;
>> OUT.DataType = 'MY_TYPE';
>> OUT.Dimensions = 1;
>> OUT.Complexity = 'real';
In the Stateflow chart, it is then necessary to enable the 'Data must resolve to signal object' option for the output variable OUT in the Model Explorer.
Finally, in the transition action, in order to match the Real World Value (RWV) behavior of the Data Type Conversion block, you can use:
OUT = cast(IN,'like',OUT);
In order to match the Stored Integer (SI) behaviour instead, you can use:
OUT = rescale(IN,OUT.Slope,OUT.Bias)
In this latter case, make sure that the Data Type of the input variable IN is set as 'Inherit: Same as Simulink'.
For further details, please refer to the attached model 'sf_casting' (R2018a).