How do I create a callback function which gets executed whenever the XLim value of axes changes?

28 views (last 30 days)
I want to create a callback function which will get executed whenever the XLim value is changed from Command Line.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jan 2010
There is no direct callback that gets executed when XLim/YLim property of the axes changes. But it is possible to create a callback function which will listen to the XLim/YLim property and get executed whenever XLim/YLim changes value.
In order to create a callback function, which gets fired when the XLim value changes, create an event listener using ADDLISTENER for the current axis. Then create a callback function in the same directory and assign it as the PostSet callback of the 'XLim' property.
The attached file, testCallback.m, and the callback function, Callbackfcn, illustrate this.
  1 Comment
Martin Vatshelle
Martin Vatshelle on 7 Feb 2019
I am sorry for revocing such an old post, but I felt it fits so well with my issue.
I tested this callback function using R2017a.
The callback is activated when I manually change XLim.
But when I call plot(ax,...) the XLim property changes without activating the calback.
I can make a wrapper function for plot to activate the callback like this:
function myPlot(varargin)
line = plot(varargin{:});
but there is a lot of calls to plot that needs to be changed, is there a better way?

Sign in to comment.

More Answers (0)


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




Community Treasure Hunt

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

Start Hunting!