matlab - set class variable in other function than constructor -
this question has answer here:
this basic question, did not find on internet. writing matlab class, has various class properties. setting of them (some input arguments, default). change 1 class variable in class method. not work (the variable gets deleted when out of function scope). best way solve this? put in constructor?
classdef myclass properties matrix1 matrix2 matrix3 end methods function obj = myclass() obj.matrix1 = zeros(2) obj.matrix2 = ones(3) end function obj = func(obj) obj.matrix2 = 3*ones(3) end function obj = func2(obj) obj.matrix3 = obj.matrix2 %this not work. matrix2 has original value, not 3*ones(3) end end end i call like
object = myclass() object.func() object.func2()
matlab supports both value-type , reference-type classes.
the way have defined class, inherently value type, means each function call uses copy of object, , not refer calling object's data.
to make class behave reference type, modify object's data require, inherit class handle.
do:
classdef myclass < handle % else same here... end
Comments
Post a Comment