Main Content

Class Does Not Have Property

If a MATLAB® class has a method, mymethod, that returns a handle class with a property, myprop, you cannot generate code for the following type of assignment:

obj.mymethod().myprop=...

For example, consider the following classes:

classdef MyClass < handle
  properties
    myprop
  end
  methods
    function this = MyClass
      this.myprop = MyClass2;
    end
    function y = mymethod(this)
      y = this.myprop;
    end
  end
end
classdef MyClass2 < handle
  properties
    aa
  end
end

You cannot generate code for function foo.

function foo
 
h = MyClass;

h.mymethod().aa = 12;
In this function, h.mymethod() returns a handle object of type MyClass2. In MATLAB, the assignment h.mymethod().aa = 12; changes the property of that object. Code generation does not support this assignment.

Solution

Rewrite the code to return the object and then assign a value to a property of the object.

function foo

h = MyClass;

b=h.mymethod();
b.aa=12;

Related Topics