主要内容

Resolve Error: coder.varsize Not Supported for Class Properties

Issue

Code generation supports unbounded variable-size properties for value and handle classes. However, you cannot define MATLAB® class properties as variable-size by using the coder.varsize directive. If you call coder.varize on a class property, this error occurs during code generation:

'coder.varsize' is not supported for class properties.

For example, consider the class MyClass, which has a single property, prop. The varSizePropError function uses the class. The function defines prop as a 1-by-10 array and declares that the size of prop varies by using coder.varsize. It then uses the property prop by passing it to the numel function. Finally, the function changes the size of the property to 1-by-n. Code generation for varSizePropError fails because the code generator does not support calling coder.varsize on class properties. However, if you remove the coder.varsize call, code generation fails because the code generator fixes the size of prop at 1-by-10 when it generates code for the numel function call. The code generator cannot change the size of prop after this point.

classdef MyClass
    properties
        prop
    end
end

function out = varSizePropError(n)
obj = MyClass;
obj.prop = 1:10;
coder.varsize("obj.prop")
numel(obj.prop)
obj.prop = 1:n;
out = obj.prop;
end

Possible Solutions

To specify that the size of a class property can change, try one of these solutions.

Assign Multiple Sizes to a Property Before You Use It

You can instruct the code generator to define a class property as variable size by assigning arrays of various sizes to it. You must do this before you use the property. For example, define prop as a 1-by-10 array and a 1-by-5 array before passing it to the numel function.

function out = varSizePropExample1(n)
obj = MyClass;
obj.prop = 1:10;
obj.prop = 1:5;
numel(obj.prop)
obj.prop = 1:n;
out = obj.prop;
end

Generate code for varSizePropExample1 and examine the code generation report. The code generator defines prop as an unbounded row vector.

Initialize Property by Using a Variable-Size Temporary Variable

Alternatively, you can instruct the code generator to define a property as variable size in the class definition by assigning the property to a variable-size local variable. Because you call coder.varsize on the local variable, not on the class property, code generation succeeds.

For example, consider the class MyVarPropClass. Define a constructor method for this class, and define a variable-size local variable in the constructor method by using coder.varsize. Assign the class property to this local variable.

classdef MyVarPropClass
    properties
        prop
    end
    methods
        function obj = MyVarPropClass
            local = 1:10;
            coder.varsize("local",[1 Inf]);
            obj.prop = local;
        end
    end
end

Because you assign property prop to a variable-size variable in the class definition, the code generator treats this property as variable size, and you do not have to change the size of property before you use it. Generate code for the function varSizePropExample2 and inspect the code generation report. The code generator defines prop as an unbounded row vector.

function out = varSizePropExample2(n)
obj = MyVarPropClass;
obj.prop = 1:10;
numel(obj.prop)
obj.prop = 1:n;
out = obj.prop;
end

See Also

Topics