MATLAB: Bizarre value class behavior

classes value problemMATLABoop

Below is a self-explanatory VALUE class I developed for purposes of illustration.
classdef ClassA
properties
x;
y;
end
methods
function obj = ClassA()
obj.x = 0;
obj.y = 0;
end
function setX(obj, val)
obj.x = val;
end
function setY(obj, val)
obj.y = val;
end
function obj = set.x(obj, val)
*obj* .x = val;
end
function obj = set.y(obj, val)
obj.y = val;
end
end
end
I instantiated it as follows:
a = ClassA();
Good. That worked. Now to test the setter methods:
a.x = 1;
a.y = 2;
Those worked too. But doing this:
a.setX(3)
a.setY(5)
causes the prompt to not echo the value of the properties as is done when the setter methods are used. Why? And when I enter the variable name in the prompt, the property values are echoed back but they do not change upon using setX and setY. This is puzzling. I followed proper VALUE class syntax for setX and setY. Help please!

Best Answer

"I followed proper VALUE class syntax for setX and setY." &nbsp No, that's handle class syntax. With a value class you create a new instance, which you need to return.
With the class definition modified
>> a = ClassA
a =
ClassA with properties:
x: 0
y: 0
>> a = a.setX( 17)
a =
ClassA with properties:
x: 17
y: 0
or
>> a = setY( a,117)
a =
ClassA with properties:
x: 17
y: 117
where
classdef ClassA
properties
x;
y;
end
methods
function obj = ClassA()
obj.x = 0;
obj.y = 0;
end
function obj = setX(obj, val)
obj.x = val;
end
function obj = setY(obj, val)
obj.y = val;
end
function obj = set.x(obj, val)
obj.x = val;
end
function obj = set.y(obj, val)
obj.y = val;
end
end
end