handlePropEvents method not recognized
7 ビュー (過去 30 日間)
古いコメントを表示
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)
0 件のコメント
回答 (1 件)
TARUN
2025 年 2 月 10 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Properties についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!