Why is event PreUpdate and PostUpdate not triggered in ChartContainer?

32 views (last 30 days)
Hi all,
I’ve created a ChartContainer in MATLAB R2024b and need to track when its update routine finishes so I can restore an internal property to its original value. During setup, I added listeners for the PreUpdate and PostUpdate events of the ChartContainer.
However, when I change a property, the update method runs, but neither the PreUpdate nor PostUpdate events seem to fire.
Below I share the class implementation:
classdef TestComponent < matlab.graphics.chartcontainer.ChartContainer
%% Properties
properties (AbortSet)
% Internal use. Indicates update is required, triggered by external
% property.
TriggerUpdate (1,1) logical = false;
end %properties
properties (Transient, NonCopyable, Hidden, SetAccess = protected, UsedInUpdate = false)
% Event listener array
EventListeners (1,:) event.listener
end
%% Protected Methods
methods (Access = protected)
function setup(obj)
% Setup routine
% Show setup routine is executed
disp('setup')
% Setup listeners to all events in the class
metaClass = meta.class.fromName(class(obj));
eventList = {metaClass.EventList.Name};
listenAcces = {metaClass.EventList.ListenAccess};
% Initialize listener to post-update event
for idx = 1:numel(eventList)
if ~strcmpi(listenAcces{idx}, {'public', 'protected'})
continue
end
obj.EventListeners(end+1) = listener(obj, ...
eventList{idx}, @(s,e) obj.showEvent(e));
end
end %function
function update(obj)
% Update routine
% Show update routine is executed
disp('update')
end %function
end %methods
methods
function showEvent(obj, e)
disp(e)
end %function
end %methods
end
Does anyone know why these events aren’t triggered? For ComponentContainer, the events work as expected.

Accepted Answer

Aditya
Aditya on 17 Dec 2025 at 6:08
Hi Jacob,
You have observed correctly: PreUpdate and PostUpdate events are available and documented for ComponentContainer (UI components), but not for ChartContainer (axes-based charts).Why aren't PreUpdate/PostUpdate firing in ChartContainer?
  • ChartContainer does not define or fire PreUpdate/PostUpdate events.
  • The MATLAB documentation for ComponentContainer explicitly lists these events, but the ChartContainer documentation does not.
  • This is by design: ChartContainer update cycle is not evented in the same way as ComponentContainer.
You can try the following steps:
  • Override the update method and place your logic before and after calling the superclass update, if needed.
  • If you need to execute code after every update, do it at the end of your update method.
  • If you have code that should run before an update, do it at the beginning of update.

More Answers (1)

Andrew Ouellette
Andrew Ouellette on 23 Dec 2025 at 23:51
I can't give you a great answer as to why the behavior differs between ChartContainer and ComponentContainer. However, I expect that you can achieve the behavior you desire without needing to create listeners to undocumented events.
In particular, you mention that you want to perform some clean up action after "update" has finished. Could you just place your clean up functionality at the bottom of the update function?
I imagine you could have an implementation like the following:
classdef TestComponent < matlab.graphics.chartcontainer.ChartContainer
methods (Access=protected)
function setup(this)
% You can often leave this method empty
end
function update(this)
preUpdate(this);
% Insert your current update code here
postUpdate(this);
end
function preUpdate(this)
% Setup transient data for update
end
function postUpdate(this)
% Cleanup transient data from update
end
end
end

Categories

Find more on Developing Chart Classes in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!