Main Content

Resolve Error: Class Does Not Have Property

Issue

When the MATLAB® code attempts to access a class method that does not exist, code generation fails with the error:

Class 'MyClass' does not have a property with name 'myMethod'.

This error also occurs if your MATLAB code uses a single expression to return a class property and assign it a new value. For example, consider a handle class named MyClass that has the method myMethod. myMethod returns a handle class that has the property myprop. In MATLAB, you can assign a value to myprop in a single step:

MyClass.myMethod().myprop = value;

When you perform this assignment in your MATLAB code, code generation fails.

Possible Solutions

To resolve this error, try one of these solutions.

Check Method Name

If the method does not exist in the specified class, create the method. If the method does exist, check the spelling in your code.

Assign Value to Method in Two Steps

If your MATLAB code assigns a value to a object property that is returned by the method of another class, you can resolve the error by performing the assignment in two steps. First, return the object, and then assign a value to the object property.

For example, consider the handle classes MyHandleClass and MyOtherHandleClass and the function useMyHandleClasses_error that uses these classes. Code generation fails for useMyHandleClasses_error because this function uses a single expression to return the property MyOtherHandleClass.prop_inner and assign it a value.

classdef MyHandleClass < handle
    properties
        prop_outer
    end
    methods
        function this = MyHandleClass
            this.prop_outer = MyOtherHandleClass;
        end
        function y = myMethod(this)
            y = this.prop_outer;
        end
    end
end

classdef MyOtherHandleClass < handle
    properties
        prop_inner
    end
end

function out = useMyHandleClasses_error(n) %#codegen
myObj = MyHandleClass;
myObj.myMethod().prop_inner = n;
out = myObj.myMethod().prop_inner;
end

To resolve this error, use a temporary variable and assign a value to inner_prop in two steps. For example, code generation for the function useMyHandleClasses succeeds.

function out = useMyHandleClasses(n) %#codegen
myObj = MyHandleClass;
temp = myObj.myMethod();
temp.prop_inner = n;
out = temp.prop_inner;
end

See Also

|

Topics