Resolve Issue: Variables Must Be Fully Defined Before Use - MATLAB & Simulink (original) (raw)
Issue
Unlike MATLABĀ®, which is a dynamically typed language, C and C++ are statically typed. This means that the code generator must be able to determine the types of all variables in your MATLAB code to define and allocate variables in the generated code. If the code generator is unable to determine the types of one or more variables in your MATLAB code, the code generator returns an error message containing this sentence:
For code generation, all variables must be fully defined before use.
If the error message that you see refers to a cell array or cell array element, see Resolve Issue: Cell Array Elements Must Be Fully Defined Before Use.
Possible Solutions
To resolve this issue, assign values to all variables in your MATLAB code on all execution paths. This includes all variables contained within other data structures, such as structure fields and class properties. Depending on your MATLAB code and the specific error message you see, try one of these solutions.
Assign Values to Variables on All Execution Paths
In certain cases, the code generator is unable to determine that all variables are defined on all execution paths. For example, create a MATLAB function undefinedVariableTest
that takes a numeric input x
and returns the absolute value ofx
.
function y = undefinedVariableTest(x) %#codegen arguments x (1,1) double end if x == 0 y = 0; elseif x < 0 y = -x; elseif x > 0 y = x; end end
In MATLAB execution, calls to undefinedVariableTest
return a value for all values ofx
. However, code generation fails forundefinedVariableTest
because the code generator cannot find an else
statement and is thus unable to determine thaty
is defined on all possible execution paths.
To make this code suitable for code generation, force the code generator to recognize that y
is defined on all possible execution paths by using anelse
statement.
function y = undefinedVariableTest(x) %#codegen arguments x (1,1) double end if x == 0 y = 0; elseif x < 0 y = -x; else y = x; end end
Alternatively, assign a dummy value to y
outside of theif
statement.
function y = undefinedVariableTest(x) %#codegen arguments x (1,1) double end y = 0; if x == 0 y = 0; elseif x < 0 y = -x; elseif x > 0 y = x; end end
Assign Values to All Structure Fields and Class Properties
You must define all variables contained within other data types on all execution paths, and you cannot change the number or format of structure fields or class properties after the structure or class has been used. In addition, for certain coding patterns, the code generator is unable to recognize that all variables contained within another data type are defined. In these situations, try rewriting your code using a different coding pattern.
Structure Fields. In MATLAB code for code generation, you cannot add fields to a structure after you read the structure, index the structure, or pass the structure to a function. For example, create a MATLAB function undefinedFieldTest
that returns a structure y
. If x
is greater than 10,undefinedFieldTest
defines and assigns a value to fieldfield1
of structure s
. Otherwise,undefinedFieldTest
defines and assigns values tofield1
and field2
of structures
.
function y = undefinedFieldTest(x) %#codegen arguments x (1,1) double end if x > 10 s.field1 = 11; else s.field1 = 12; s.field2 = 12; end y = s; end
In MATLAB execution, field field2
is dynamically added to structure s
if x
is 10 or less. However, the code generator must determine the types of all fields contained by structures
at code generation time, when the run-time value ofx
is unknown. Code generation fails because the code generator detects that field field2
is part of structures
on some execution paths but not others.
To make this code suitable for code generation, do not change the number or names of fields contained by a structure at run time. Define all fields of s
independent of the run-time value of x
.
function y = undefinedFieldTest(x) %#codegen arguments x (1,1) double end s = struct("field1", [], "field2", []); if x > 10 s.field1 = 11; else s.field1 = 12; s.field2 = 12; end y = s; end
To learn more about using structure data types in MATLAB code for code generation, see Structure Definition for Code Generation.
Class Properties. For certain coding patterns in which you assign values to class properties in a loop, the code generator is unable to recognize that all properties are defined. For example, create a MATLAB function undefinedPropTest
that takes a positive integer input n
and returns an instance of classMyClass
. Class MyClass
has two properties, prop1
and prop2
. FunctionundefinedPropTest
assigns values toprop1
and prop2
for the returnedmyClass
object inside afor
-loop.
function y = undefinedPropTest(n) %#codegen arguments n (1,1) double {mustBePositive, mustBeInteger} end x = MyClass; for i = 1:n x.prop1 = 1 + i; x.prop2 = x.prop1 + 3; end y = x; end
In MATLAB execution, undefinedPropTest
returns an instance of MyClass
for all allowed values of n
. However, code generation for undefinedPropTest
fails because the code generator is unable to determine thatundefinedPropTest
assigns values toprop1
and prop2
for all values of n
.
To make this code suitable for code generation, assign dummy values to all properties of MyClass
before thefor
-loop.
function y = undefinedPropTest(n) %#codegen arguments n (1,1) double {mustBePositive, mustBeInteger} end x = MyClass; x.prop1 = 0; x.prop2 = 0; for i = 1:n x.prop1 = 1 + i; x.prop2 = x.prop1 + 3; end y = x; end
For additional considerations when defining class properties in MATLAB code for code generation, see Defining Class Properties for Code Generation.
Assign Initial Values to All Persistent Variables
You must define allpersistent variables in your MATLAB code on all execution paths. For example, create a MATLAB function undefinedPersistentTest
that stores the number of times it has been called in the persistent
variable count
. To initialize or resetcount
, undefinedPersistentTest
must be called with an argument of 0
.
function y = undefinedPersistentTest(x) persistent count; if x == 0 count = 0; else count = count + 1; end y = count; end
MATLAB automatically sets a persistent variable equal to an empty matrix ([]
) upon first encounter. Therefore, calls toundefinedPersistentTest
do not produce an error, even when count
has been given a value. However, the code generator is unable to determine the size and type of an uninitialized persistent variable at code generation time. Therefore, code generation forundefinedPersistentTest
fails becausecount
is not defined for all values of x
.
To make this code suitable for code generation, use the isempty function to assign a value to count
if this variable is not defined.
function y = undefinedPersistentTest(x) arguments x (1,1) double end persistent count; if isempty(count) count = 0; end if x == 0 count = 0; else count = count + 1; end y = count; end
Define Variables Without Assignment
In certain situations, the overhead associated with defining variables by assignment in the generated code is significant. Defining variables by assignment can also cause redundant copies of variables to appear in the generated code. To define variable type, size, and complexity without the overhead of variable assignment, use coder.nullcopy.
If you use coder.nullcopy
to define a variable, you can generate code without having to adhere to the recognized coding patterns described above. For example, consider the functionnullcopyExample
. Because this function usescoder.nullcopy
to preallocate memory for classMyClass
, you do not have to assign dummy values to the properties of this class.
function y = nullcopyExample(n) %#codegen arguments n (1,1) double {mustBePositive, mustBeInteger} end x = coder.nullcopy(MyClass); for i = 1:n x.prop1 = 1 + i; x.prop2 = x.prop1 + 3; end y = x; end
Note
Use coder.nullcopy
with caution. You must make sure that you assign values to all variables, including class properties, structure fields, and array elements. If you access uninitialized variables, the results can be unpredictable.