The issue is because the handlePropEvents method is not connected to the observable properties. In MATLAB, when you want to react to changes in observable properties, you need to add a listener “addlistener” to those properties in the constructor of the class.
The handlePropEvents method won't be automatically called when a property changes if there are no property listeners.
You can learn more about the listeners here: https://www.mathworks.com/help/matlab/matlab_oop/listening-for-changes-to-property-values.html
Here is the updated code of both classes with listeners:
classdef Plot < handle
properties(SetObservable)
titleString = 'No Title';
xLabel = 'xLabel';
end
methods
function obj = Plot(figureSize)
addlistener(obj, 'titleString', 'PostSet', @Plot.handlePropEvents);
addlistener(obj, 'xLabel', 'PostSet', @Plot.handlePropEvents);
end
end
methods(Static)
function handlePropEvents(src, event)
obj = event.AffectedObject;
switch src.Name
case 'titleString'
title(obj.titleString);
case 'xLabel'
xlabel(obj.xLabel);
end
end
end
end
classdef TimeSpace < Plot
properties
end
methods
function obj = TimeSpace(lineTable)
% Call the parent class constructor
obj@Plot(lineTable.data);
obj.setEnvironment(lineTable.data);
end
function setEnvironment(obj, line)
obj.titleString = 'Abbildung A.14';
obj.xLabel = 'Stunden';
end
end
end
With this code the Title and x-label will be updated due to the property listeners.
