Resolve Error: Class Properties Must Be Fully Defined Before Use
Issue
Because C and C++ are statically typed languages, the code generator must be able to determine the types of all variables in the MATLAB® code so that it can define the variables in the generated code. For certain coding patterns, the code generator can be unable to recognize that all class properties have assigned values. These coding patterns include:
Assigning values to class properties inside a loop.
Copying a handle class property to another object.
If the code generator is unable to recognize that you define all of the properties of a MATLAB class, you see an error message containing this sentence:
For code generation, all class properties
must be fully defined before use.
Possible Solutions
Depending on the coding pattern that you use, try one of these solutions.
Assign Values to Class Properties in a Loop
When you assign values to class properties in a loop, the code generator can be
unable to recognize that all properties have
assigned values. For example, this MATLAB function takes a positive integer input in and
returns an instance of class MyClass. The class
MyClass has two properties, prop1 and
prop2. The function undefinedPropTest
assigns values to prop1 and prop2 for the
returned myClass object inside a
for-loop.
function y = undefinedPropTest(in) %#codegen arguments in (1,1) double {mustBePositive, mustBeInteger} end x = MyClass; for i = 1:in 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 in. 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 in.
To generate code for this function, 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
Copy Handle Class Property to Another Object
When you copy a handle object property to another object, the code generator can
be unable to determine that the MATLAB code defines the copied property. For example, consider the function
refHandleTest, which uses the handle class
myHandleClass and the value class
myValueClass. In this function, three variables reference the
same memory location, x, y, and
v.prop.
function [out1, out2, out3] = refHandleTest(n) %#codegen x = myHandleClass; y = x; v = myValueClass(); v.prop = x; x.prop = n; out1 = x.prop; out2 = y.prop; out3 = v.prop.prop; end classdef myHandleClass < handle properties prop end end classdef myValueClass properties prop end end
Code generation for refHandleTest fails because the code
generator cannot determine that the handle object property
v.prop.prop points to the same memory location as
x.prop and y.prop. To resolve this error,
define v.prop.prop
directly.
function [out1, out2, out3] = refHandleTest(n) %#codegen x = myHandleClass; y = x; v = myValueClass(); v.prop = x; x.prop = n; v.prop.prop = n; out1 = x.prop; out2 = y.prop; out3 = v.prop.prop; end