To generate embeddable code, the Embedded MATLAB Function blockrelies on an analysis that determines the size, class, and complexityof each variable. This analysis imposes the following restrictions:
-
The first assignment to a variabledefines its, size, class, and complexity.
See Working with Variables inthe Embedded MATLAB documentation.
-
Generally, you cannot reassign variable propertiesafter the initial assignment except when using variable-size dataor reusing variables in the code for different purposes.
See When You Can Reassign Properties in Embedded MATLAB Code inthe Embedded MATLAB documentation
In the MATLAB language, variables change their propertiesdynamically so you can use the same variable to hold a value of anyclass, size, or complexity. For example, the following code worksin MATLAB:
if(c>0) x = int8(0); else x = [1 2 3]; end % The type of 'x' here depends on % the value of 'c'. MATLAB handles this. disp(x);
In Embedded MATLAB functions, you must assign variablesexplicitly to have a specific class, size, and complexity before usingthem in operations or returning them as outputs . For more information,see Assign Variables Explicitly Before Using Them.
When You Can Reassign Properties in Embedded MATLAB Code
There are certain cases when you can reassign a variable witha value of different class, size, or complexity:
-
Dynamically sized variables
A variable can hold values that have the same class and complexitybut different sizes. If the size of the initial assignment is notconstant, Embedded MATLAB makes a variable dynamically sized.For more information, see Working with Variable-Size Data.
-
Variables reused in the code for different purposes
If a variable is reused in the code in such a way that it holdsvalues of different class, size, or complexity in different partsof your source code, Embedded MATLAB attempts to rename the variablein each part of the code to create multiple independent variables.For more information, see Reusing the Same Variable with Different Properties.
-