MATLAB: Listen for Changes to Property Values

MATLABoop

I would like to update the RMS object property always when the signal object property has change:
classdef track < handle
properties
rms = 0;
end
properties (SetObservable)
signal = [1 2 3 4 5 6 7 8 9 10];
end
methods
function obj = update_rms(obj)
obj.rms = rms(obj.signal);
end
end
end
EXAMPLE:
% Init object:
myObj = track();
% Signal property manual update:
myObj.signal = [1 1 1 1];
% After this, I would like that myObj update automatically the RMS property calling the update_rms method.
Could you help me?

Best Answer

If I understood correctly, wouldn't the following be the simplest way to implement what you want:
classdef (ConstructOnLoad) track < handle
properties (SetAccess = private) %I assume that rms cannot be set by the user
rms;
end
properties
signal = 1:10;
end
methods
function this = track()
this.UpdateRms();
end
function set.signal(this, value)
this.signal = value;
this.UpdateRms();
end
end
methods (Access = private)
function UpdateRms(this)
this.rms = rms(this.signal);
end
end
end
No need for dependent properties or listener. Just recalculate rms whenever signal is updated.
The ConstructOnLoad is there to ensure that rms is recalculated when a class object is reloaded from disk. It's not strictly necessary since rms and signal should never be out of sync even when read from disk.