Why do I need to specify the datatype when I assign a variable in Embedded MATLAB block in Simulink 6.3 (R14SP3)?

3 次查看(过去 30 天)
I am using an Embedded MATLAB block in Simulink 6.3 (R14SP3). I have defined an expression:
m = int16(1);
m = 10;
When I run the model Simulink returns the following error message:
Class mismatch (int16 ~= double). The class to the left is the class of the left-hand side of the assignment.

采纳的回答

MathWorks Support Team
This is expected behavior for the Embedded MATLAB Function Block.
In Embedded MATLAB once a variable is initialized its size and type may not be changed. Since the default type for a scalar is double the code
m = int16(1);
m = 10;
will error out because 10 is of type double and m is of type int16.
You can recast values for variable assignment in two ways.
1) Explicitly recast the value with the appropriate typcast function:
m = int16(1);
m = int16(10);
2) Implicitly recast the value using the colon operator:
m = int16(1);
m(:) = 10;
For more information on variable initialization and assignment in Embedded MATLAB Function Blocks see the documentation section titled: "Embedded MATLAB Coding Style", or you can execute the following in an MATLAB R2008a command window:
web([docroot '/toolbox/eml/ug/brdqvz_.html#bq2l74g']);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by