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

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 2009
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']);

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos


Versión

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by