MATLAB: Update a value class property only if required

handle classMATLABobject-orientedvalue class

Hello everyone,
I am new to object oriented programming and have some issues getting the behavior of my class in the way I want it. I have several input properties and a "dependent" output property. Because the algorithm to compute the output is numerically intensive, I do not want to handle it as an actual dependent property. It should only be updated if necessary. Also I want to be able to save my results to my hard drive, which would not work with a dependent property.
I got this basically working if my class is a handle class as shown in the example down below. But I do not like how handle classes function. In particular it can be very unintuitive to the user if they try something like this:
A = myclass(1,2);
B = A;
A.input1 = 5;
So here is my example with the handle class. Can anyone tell me if that can also be achieved with a value class? General improvement suggests are welcome as well 🙂
Thanks a lot in advance!
classdef myclass < handle
properties
input1
input2
output
update_required
end
methods
function obj = myclass(value1, value2)
obj.input1 = value1;
obj.input2 = value2;
obj.output = [];
end
function set.input1(obj, value)
if obj.input1 == value
return
end
obj.input1 = value;
obj.update_required = true;
end
function set.input2(obj, value)
if obj.input2 == value
return
end
obj.input2 = value;
obj.update_required = true;
end
function value = get.output(obj)
if obj.update_required
disp('Update required.')
obj.output = obj.input1 + obj.input2;
obj.update_required = false;
else
disp('No update required.')
end
value = obj.output;
end
end
end

Best Answer

I think my solution works if you reset your update_required within your if...end block.
classdef myclass
properties
input1
input2
end
properties (Dependent = true)
output
update_required
end
properties (Hidden = true)
output_
update_required_
end
methods
function obj = myclass(value1, value2)
obj.input1 = value1;
obj.input2 = value2;
obj.output = value1 + value2;
end
function obj = set.input1(obj, value)
if obj.input1 == value
obj.update_required = false;
return
end
obj.input1 = value;
obj.update_required = true;
end
function obj = set.input2(obj, value)
if obj.input2 == value
obj.update_required = false;
return
end
obj.input2 = value;
obj.update_required = true;
end
function value = get.output(obj)
if obj.update_required
disp('Update required.')
obj.output = obj.input1 + obj.input2;
obj.update_required = false;
else
disp('No update required.')
end
value = obj.output_;
end
function obj = set.output(obj, val)
obj.output_ = val;
end
function val = get.update_required(obj)
val = obj.update_required_;
end
function obj = set.update_required(obj, val)
obj.update_required_ = val;
end
end
end
>> A = myclass(1, 2);
>> A
A =
Update required.
myclass with properties:
input1: 1
input2: 2
output: 3
update_required: 1
>> A.input1 = 1
A =
No update required.
myclass with properties:
input1: 1
input2: 2
output: 3
update_required: 0
>> A.input2 = 2
A =
No update required.
myclass with properties:
input1: 1
input2: 2
output: 3
update_required: 0
>> A.input1 = 5
A =
Update required.
myclass with properties:
input1: 5
input2: 2
output: 7
update_required: 1
>> A.input2 = 10
A =
Update required.
myclass with properties:
input1: 5
input2: 10
output: 15
update_required: 1