4

Just started crash coursing in Matlab OO programing and I would like to write a set method for a object that will set the value then reciprocate by setting itself in the relevant field on the other object.

classdef Person
properties
  age;
  sex;
  priority; % net priority based on all adjustment values
  adjustment; % personal adjustment value for each interest
  family;
end

methods
  function obj = set.sex(obj, value)
    if value == 'm' || value == 'f'
      obj.sex = value;
    else
      error('Sex must be m or f')
    end
  end

  function obj = set.family(obj,value)
    if class(value) == 'Family'
      obj.family = value;
    else
      error('Family must be of type Family')
    end
  end
end
end



classdef Family
properties
  husband;
  wife;
  children;
  elders;
  adjustment; % interest adjustment values
end

methods
  function this = set.husband(this,person)
    if class(person) == 'Person'
      this.husband = person;
      person.family = this;
    else
      error('Husband must be of type Person')
    end
  end

  function this = set.wife(this,person)
    if class(person) == 'Person'
      this.wife = person;
      person.family = this;
    else
      error('Wife must be of type Person')
    end
  end
end
end

So what I have to do now is:

p = Person
f = Family
f.husband = p
p.family = f

What I would like is for family and person to auto set themselves in each other:

p = Person
f = Family
f.husband = p

And Family set.husband function will set p's family value to f. Why is my code not working? As far as I can tell I'm doing what is suggested in the comments.

Edit: After some messing around I've confirmed that "this" and "person" are objects of the correct type. Ultimately the issue is that Matlab passes by value rather then by reference. Unless anyone knows a way around that I'll answer myself when I can.

csleys
  • 45
  • 6
  • Use a setter method like `setHusband(p)` in `Family` class, inside, set the `husband` property of the current `Family` object as well as setting the `family` property of the input `p` object. – Bee Jul 17 '13 at 14:42
  • I attempted more or less the same thing, see code. Unfortunately it is not working. The above codes will set the family value but does not set the person value. Any idea about why that may be? – csleys Jul 17 '13 at 14:49
  • 2
    I see. Maybe making the `Person` and `Family` classes `handle` objects solves the pass by reference problem? Just add `< handle` to the definition of the classes: http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html – Bee Jul 17 '13 at 15:32
  • That did the trick. Thank you very much good sir. Go ahead and post an answer so I can give you credit. – csleys Jul 17 '13 at 15:40

1 Answers1

5

Normal objects are usually considered value objects. When they are passed to a function or a method, only the value is passed not a reference to the original object. Matlab may use a read-only referencing mechanism to speed things up, but the function or method cannot change the properties of the original object.

To be able to pass an input parameter by reference, your custom object needs to be a handle object. Simply when defining your class, inherit from handle and that should do the trick:

classdef Person < handle

and

classdef Family < handle
Bee
  • 2,472
  • 20
  • 26