Setting an object property using a method in Matlab

I am creating a class in MATLAB, and although I have little experience with objects, I am pretty sure that I must set the class property using the class method. Is this possible in MATLAB?

classdef foo properties changeMe end methods function go() (THIS OBJECT).changeMe = 1; end end end f = foo; f.go; t.changeMe; ans = 1 
+7
source share
1 answer

Yes it is possible. Note that if you create a value object, the method must return the object in order to change the property (since value objects are passed by value). If you create a handle object ( classdef foo<handle ), the object is passed by reference.

 classdef foo properties changeMe = 0; end methods function self = go(self) self.changeMe = 1; end end end 

As mentioned above, a call to the method of setting the value object returns a modified object. If you want to modify the object, you need to copy the output back to the object.

 f = foo; f.changeMe ans = 0 f = f.go; f.changeMe ans = 1 
+9
source

All Articles