How can I typecast a variable to a custom Typedef in generated code?
7 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2019 年 5 月 13 日
編集済み: MathWorks Support Team
2019 年 6 月 19 日
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?
採用された回答
MathWorks Support Team
2019 年 6 月 19 日
編集済み: MathWorks Support Team
2019 年 6 月 19 日
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.
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!