Hi
I’m writing a class for the first time, and got an issue I don’t understand.
To simplify the problem, assume my class is as follows:
======================================== classdef testClass properties count end methods function obj = testClass obj.count = 1; end function obj = increment(obj) obj.count = obj.count + 1; end end end ======================================== >> x = testClass;
This creates the class and sets x.count to 1 as required
I then want to increment the value of x.count I was hoping that:
>> x.increment;
would add 1 to count. But it simply creates a new object (‘ans’), where ans.count == 2
I can of course write,
>> x = x.increment;
But that feels illogical.
I've tried various things, and Googled the type of problem, but have found a solution. So any guidance would be welcome.
Thanks. Martyn
Best Answer