matlab - Self reference within an object method -
just started crash coursing in matlab oo programing , write set method object set value reciprocate setting in relevant field on other object.
classdef person properties age; sex; priority; % net priority based on adjustment values adjustment; % personal adjustment value each interest family; end methods function obj = set.sex(obj, value) if value == 'm' || value == 'f' obj.sex = value; else error('sex must m or f') end end function obj = set.family(obj,value) if class(value) == 'family' obj.family = value; else error('family must of type family') end end end end classdef family properties husband; wife; children; elders; adjustment; % interest adjustment values end methods function = set.husband(this,person) if class(person) == 'person' this.husband = person; person.family = this; else error('husband must of type person') end end function = set.wife(this,person) if class(person) == 'person' this.wife = person; person.family = this; else error('wife must of type person') end end end end
so have is:
p = person f = family f.husband = p p.family = f
what family , person auto set in each other:
p = person f = family f.husband = p
and family set.husband function set p's family value f. why code not working? far can tell i'm doing suggested in comments.
edit: after messing around i've confirmed "this" , "person" objects of correct type. issue matlab passes value rather reference. unless knows way around i'll answer myself when can.
normal objects considered value
objects. when passed function or method, value passed not reference original object. matlab may use read-only referencing mechanism speed things up, function or method cannot change properties of original object.
to able pass input parameter reference, custom object needs handle
object. when defining class, inherit handle
, should trick:
classdef person < handle
and
classdef family < handle
Comments
Post a Comment