handlePropEvents method not recognized

3 views (last 30 days)
Lukas Ballo
Lukas Ballo on 17 Mar 2022
Answered: TARUN on 10 Feb 2025
Dear MATLAB Community
I have a class Plot with observable properties and a handlePropEvents static method. However, when I create an instance of a derived class TimeSpace and change a property there, MATLAB returns a warning that Plot.handlePropEvents is undefined. Does someone have any idea what could be the problem?
Thanks for any hints.
This is the base class:
classdef Plot < handle
properties(SetObservable)
titleString = 'No Title';
xLabel = 'xLabel';
end
methods
function obj = Plot(figureSize)
% Some code here
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
And this is the derived class where we change the properties:
classdef TimeSpace < Plot.Plot
properties
end
methods
function obj = TimeSpace(lineTable)
obj@Plot.Plot();
obj.setEnvironment(lineTable.data);
end
function setEnvironment(obj, line)
obj.titleString = 'Abbildung A.14'
obj.xLabel = 'Stunden';
end
end
end
This is the warning (LoadCarped is just another derived class from TimeSpace):
Warning: Error occurred while executing the listener callback for the Plot.LoadCarpet class xLabel
property PostSet event:
Undefined function 'Plot.handlePropEvents' for input arguments of type 'meta.property'.
> In Plot/TimeSpace/setEnvironment (line 28)
In Plot/TimeSpace (line 17)
In Plot.LoadCarpet (line 13)
In Plot.plotAll (line 51)
In Scenario/runPlots (line 103)
In Scenario/runAnalysis (line 97)
In Scenario/runShunting (line 91)
In Scenario/runDemand (line 85)
In Scenario/runVehicles (line 74)
In Scenario/runTimetable (line 68)
In Scenario/run (line 44)
In Scenario (line 39)
In main (line 12)

Answers (1)

TARUN
TARUN on 10 Feb 2025
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 addlistenerto those properties in the constructor of the class.
ThehandlePropEvents method won't be automatically called when a property changes if there are no property listeners.
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.

Categories

Find more on Properties in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!