MATLAB CLASSES

I come from a Java background. I'm having trouble with classes in Matlab, especially getters and setters. getting a message saying there was a conflict between a descriptor and a value class, I was a bit lost that any help would be helpful for this for lack of a better word.

classdef Person
properties(Access = private)
    name;
    age; 
end


methods
    % class constructor
    function obj = Person(age,name)         
             obj.age = age;
             obj.name = name;
    end

    %getters
    function name = get.name(obj)          
    end

    function age = get.age(obj)
    end

    %setters
    function value = set.name(obj,name)
    end

    function value = set.age(obj,age)
    end

end

      

end

+3


source to share


2 answers


Implementation

Since your class is currently a subclass of the default Value class, your setters need to return the modified object:

function obj = set.name(obj,name)
end
function obj = set.age(obj,age)
end

      

From the documentation : "If you pass a [value class] to a function, the function should return a modified object." And in particular : "In value classes, methods ... that modify an object must return a modified object to copy over an existing object variable."


The handle keys ( classdef Person < handle

) do not need to return a modified object (such as return void

):

function [] = set.name(obj,name)
end
function [] = set.age(obj,age)
end

      

Value versus handle



More deeply, the difference between the Value class and the Handle class is mainly assignment:

  • Assigning an instance of a class to a variable creates a copy of that class.
  • Assigning an instance of the Handle class to a variable creates a reference (alias) for that instance.

Mathworks has a good rating on this topic. To paraphrase their illustration, the behavior of the Value class

% p  is an instance of Polynomial
p = Polynomial(); 
% p2 is also an instance of Polynomial with p state at assignment
p2 = p;

      

and the Handle class

% db is an instance of Database
db = Database();
% db2 is a reference to the db instance
db2 = db;

      

+6


source


Quick'n Dirty from a Java perspective: - "handle" classes are what your mind is tuned for. correct object instances with pointers to them. use them. - the "value" classes always return a full clone of any object (which has been modified by what you just did, such as setting a name).

the reason they are in Matlab is because in Matlab you expect "value" behavior natively. Imagine you have a matrix A = [1 2; 3 4]

, then assign that via B = A

. if you install now B(1) = -1

, you hope there is A(1)

1 left, right? this is because Matlab keeps track of the "copies" and actually creates them when different variables originally set to the same matrix change. in OOP, you A(1)=-1

now have everything you need to reference an object.

In addition, native matlab routines do not have a "this / self / me" variable that contains an instance reference for access from within the functions. instead, the convention is that an instance of the class will be added to the function's argument list. so to call the function, the myclass.mymethod(arg1,arg1)

declaration must be

function mymethod(this, arg1, arg2)
  % Note that the name you choose for "this" is arbitrary!
end

      

Please note this is a java perspective (and also my favorite), the above function call is equivalent mymethod(myclass,arg1,arg1)

. this is more matlab-style native, but somehow makes it difficult to see the object method.



now, regarding the setters / getters: for the handle classes, everything now feels java-ish:

classdef MyClass < handle

properties
   MyProp;
end

methods
   function set.MyProp(this, value)  %Note: setMyProp is also valid!
       ... % do checks etc, trigger calls, 
       this.MyProp = value; 
   end

   function value = get.MyProp(this)
       ... % notify, update, triggers etc
       value = this.MyProp; 
   end 
end

      

Of course, it goes without saying that you don't need to define a receiver if you just want to return a value, i.e. myclassinstance.MyProp

will work without any error.

Finally, getters / setters for value classes are something that [never met me / I never needed] for 7 years of Matlab OOP, so I would suggest going with manual classes and enjoying happy coding in MATLAB :-) otherwise case the above explanation and the matlab official docs do the job for the getter / setters of the value class.

0


source







All Articles