Separate tools setting callbacks for one event

Consider two tools that add annotations to a plot. Both toolA and toolB add annotations that are sensitive to the current YLim of the axis. toolA and toolB are completely independent. Either one or both may be used on any given plot.
Both tools add annotations that must be placed relative to one another within their set. For example, a series of labels that "stair step" down as the x-axis increases. So the position of any individual label has some dependence on the "previous" label (text object).
I have found that just adding a listener on YLim PostSet is insufficient, as the "Restore View" for zoom does not stimulate that callback. I have found I also need the same callback(s) invoked if the figure size is changed. I am aware of the LimitsChanged callback for axis objects and the SizeChanged callback for the figure objects, but as far as I can tell they can only point to a single callback function, not a list of functions.
I have figured out some rather hacky ways to handle this use case, but I'm wondering if there is a preferred method for doing something like this. It seems that addlistener would be the preferred way as that doesn't overwrite a property, but I've been unable to find a way to get that to reliably work for all use cases (like zooming and figure resizing).
[Note that I am currently using R2022b, but there are others in our group that currently need to use MATLAB as old as R2018b. We have a number of mission-critical tools that need to be recertified if the version of MATLAB changes, so there can be lags adopting the lastest version of MATLAB.]

2 Comments

Please demonstrate the issue with code.
@Matt J, what would you like demonstrated? Is it my claim that the listener on YLim PostSet isn't stimulated by the "Restore View" action? There is another issue that describes this behavior, see: 'Restore View' of Axes does not notify 'PostSet' property listener
The question here is what is the recommended way to deal with two independent objects both having a dependency on the same event, like an update to the YLim or its size on the screen.
Newer versions of MATLAB have added features like the LimitsChangedFcn property of an axis. But they only support one callback function, not a list. So if one tool sets it to one function, and the next sets it to another, the first tool will never get its callback invoked.
I the other issue mentioned above, @Walter Roberson suggests setting the LimitsChangedFcn attribute to a function that will broadcast an event that has its own listeners. I'm not sure how to do that, but I can look into it. Unfortunately his comment has no follow-ups so I can't guage if that is a "preferred" way of doing this.
There seems to be several ways to address this, but I'm trying to implement in a way that isn't fragile or overly complicated.

Sign in to comment.

Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 30 Mar 2026 at 18:42

Commented:

on 31 Mar 2026 at 21:44

Community Treasure Hunt

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

Start Hunting!