EventListener for plot's YLim property

Hi everyone,
currently playing around with event listeners in MATLAB Apps
I am trying to get a function to execute when the limits of a plot automatically update. Aka, I'm trying to piggyback off the native automated plot limit setting in matlab plots (with streaming data) to run another function only when needed.
This is where I've gotten upto now. The listener is created correctly but it appears the function is not ultimtely called/executed.
% Add a listener to the YLim property of the DynoLiveAxes object.
% When the YLim property changes, the app.DynoLivePlotAxesChanged function is called.
app.YDynoAxisListener = addlistener(app.DynoLiveAxes, 'YLim', 'PostSet', @app.DynoLivePlotAxesChanged);
Could it be that the YLim property is simply not adfdressable as an 'event name' for use in a listener?
YLim is stored as an array. I suppose I could extract the array at every instance and run a listener on that variable, though this wouldn't solve the core problem of running as little code as possible at every instance (potentially 25600 Hz)
Thanks

6 Comments

I wonder if it would work to listen on the Limit property of app.DynoLiveAxes.YAxes ?
There have to be two input arguments for listener functions
app.YDynoAxisListener = addlistener(app.DynoLiveAxes, 'YLim', 'PostSet', @(src,evt)app.DynoLivePlotAxesChanged(src, evt));
You will have to adjust the app.DynoLivePlotAxesChanged to accomodate src and event input arguments. Just add breakpoint inside the function to test if it's properly called.
function app.DynoLivePlotAxesChanged(app, src, evt) % maybe only last two args, not sure
% code
end
Attaching a Limits listener to the YAxis NumericRuler doesn't seem to be possible:
ax = gca();
propListener = addlistener(ax.YAxis, 'Limits', 'PostSet', @listenerfun);
Error using matlab.graphics.axis.decorator.NumericRuler/addlistener
While adding a PostSet listener, property 'Limits' in class 'matlab.graphics.axis.decorator.NumericRuler' is not defined to be SetObservable.
>I wonder if it would work to listen on the Limit property of app.DynoLiveAxes.YAxes ?
I have tried so in the first place, but it gave me an error. I eblieve it was because YLim returns a 1x2 array which the listener cannot discern.
app.YDynoAxisListener = addlistener(app.DynoLiveAxes, 'YLim', 'PostSet', @app.DynoLivePlotAxesChanged);
is equivalent to
app.YDynoAxisListener = addlistener(app.DynoLiveAxes, 'YLim', 'PostSet', @(varargin)app.DynoLivePlotAxesChanged(varargin{:}));
except faster.
That is, the @ form accepts however many parameters are made available, and passes all of them to the indicated function.
It is not necessary to use
app.YDynoAxisListener = addlistener(app.DynoLiveAxes, 'YLim', 'PostSet', @(src,evt)app.DynoLivePlotAxesChanged(src,evt));
-- not unless you specifically want MATLAB to error if the event manager does not pass exactly two parameters to the handle.
Example:
fun = @testfunction
fun = function_handle with value:
@testfunction
fun()
no parameters passed
fun('first')
one parameter passed. It was: first
fun('first', 'second')
two parameters passed. They were: first second
fun('first', 'second', 'third') %expect error due to too many parameters
Error using solution>testfunction
Too many input arguments.
function testfunction(arg1, arg2)
if nargin < 1
fprintf('no parameters passed\n')
elseif nargin < 2
fprintf('one parameter passed. It was:\n'); disp(arg1);
elseif nargin < 3
fprintf('two parameters passed. They were:\n'); disp(arg1); disp(arg2);
else
fprintf('Welp! How did this happen? Should not have been able to receive %d parameters!\n', nargin);
end
end
You can see that the simple @ function handle was fully compatible with passing in up to as many parameters as the receiving function is defined to accept.

Sign in to comment.

Answers (1)

Example below
fig = uifigure();
ax = uiaxes(fig);
plot(ax, magic(3))
propListener = addlistener(ax, 'YLim','PostSet',@(src, evt)listenerfun(src, evt));
function listenerfun(src, evt)
% code
end

4 Comments

This listener doesn't get called "when the limits of a plot automatically update."
Example:
fig = uifigure();
ax = uiaxes(fig);
plot(ax, magic(3))
propListener = addlistener(ax, 'YLim','PostSet',@(src, evt)listenerfun(src, evt));
ax.YLim = [0 20]; % listener is called
ax.YLimMode = 'auto'; % YLim are updated, but listener is not called
plot(ax,1:100); % YLim are updated, but listener is not called
This is some internal MATLAB functionality where plot function might disable event triggering.
I added listener to UserData, and supplied it as an argument in plot function and the event has not been triggered.
plot(ax, 1:100, "UserData", 1)
I don't see any benefits in this because it has to be called after every single plot, but this will call listener even without changing the property values.
updateAx = @(ax) set(ax, 'YLim', ax.YLim)
updateAx(ax)
I cannot demonstrate uifigure / uiaxes here on Answers; the figure / axes version is not triggering the event when ylim changes.
fig = figure();
ax = axes(fig);
propListener = addlistener(ax, 'YLim','PostSet',@(src, evt)listenerfun(src, evt));
fprintf('before first plot\n')
before first plot
plot(ax, magic(3))
fprintf('after first plot before drawnow\n')
after first plot before drawnow
drawnow()
fprintf('after first drawnow, before second plot\n')
after first drawnow, before second plot
plot(ax, 1:5, rand(1,5)*10) %implicitly changes y limit
fprintf('after second plot, before second drawnow\n')
after second plot, before second drawnow
drawnow()
fprintf('after second drawnow\n')
after second drawnow
function listenerfun(src, evt)
fprintf('listener here!\n')
disp(evt)
end
R2023b using uifigure and uiaxes, the output is
before first plot
after first plot before drawnow
after first drawnow, before second plot
after second plot, before second drawnow
after second drawnow
so again the event does not get triggered.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 24 Oct 2023

Edited:

on 31 Oct 2023

Community Treasure Hunt

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

Start Hunting!