MATLAB: Matlab OOP, dependent class properties, correct coding techiques

dependent propertiesMATLABoop

Hi,
I have a conceptual problem I'm struggling with and was wondering if anyone could give some insight into this.
Essentially, I have a class with three (or more) properties. Watered down Pseudo code below:
classdef obj
Properties
A
B
C
end
Methods
constructor etc
function set.A
C = f(obj.A,obj.B) % C is dependent on A and B (MLINT warns on this line)
A = inputValue % Set A's value
end
end
end
Now herein lies the problem as I'm sure you have already noticed. I'm setting one properties value while changing another property's value. MLINT warns immediately that this is bad and correctly states that one should make C a dependent property.
This is fine unless speed is an issue. C is going to be called "lots of times". And having Matlab calculate it every time
*get.C*
is called is expensive. A and B will be updated through out the life of the object and hence I want C recalc'ed on the fly when they do.
Should I disregard MLINT (and probably good coding practice)?
Is there a better but still fast way to achieve what I want?
Regards, Phil

Best Answer

I generally just ignore this warning - you can turn it off for the line, file or all files (I've done the latter).
In your scenario, there's probably not an issue, but there are situations when there could be.
The best practise would be to use events & listeners - add a listener that waits for A to change. You can even use the built in event PreSet/PostSet. Search for "Listen for Changes to Property Values" in the documentation.
Indeed you do not want to make C dependent. In that scenario, you would not even use the first line
C = f(obj.A, obj.B)
because C cannot be set (in the normal sense)