Main Content

Resolve Issue: Variables Must Be Fully Defined Before Use

Issue

Unlike MATLAB®, which is an untyped 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 correctly identify and allocate variables in the generated code. If the code generator is unable to determine the types of all 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 correctly 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 of x.

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 correctly return a value for all values of x. However, code generation fails for undefinedVariableTest because the code generator cannot find an else statement and is thus unable to determine that y 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 an else 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 the if 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 field field1 of structure s. Otherwise, undefinedFieldTest defines and assigns values to field1 and field2 of structure s.

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 structure s at code generation time, when the run-time value of x is unknown. Code generation fails because the code generator detects that field field2 is part of structure s 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 class MyClass. Class MyClass has two properties, prop1 and prop2. Function undefinedPropTest assigns values to prop1 and prop2 for the returned myClass object inside a for-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 correctly 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 that undefinedPropTest assigns values to prop1 and prop2 for all values of n.

To make this code suitable for code generation, assign dummy values to all properties of MyClass before the for-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 all persistent 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 reset count, 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 to undefinedPersistentTest 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 for undefinedPersistentTest fails because count 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 generate redundant copies of variables in the generated code. To define variable type, size, and complexity without the overhead of variable assignment, use coder.nullcopy.

See Also

|

Related Topics