EventListener for plot's YLim property
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 24 de Oct. de 2023
Editada: Walter Roberson
el 24 de Oct. de 2023
I wonder if it would work to listen on the Limit property of app.DynoLiveAxes.YAxes ?
In some cases SizeChangedFcn might help, https://www.mathworks.com/help/matlab/creating_guis/manage-programmatic-app-resize-behavior.html
Mario Malic
el 24 de Oct. de 2023
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);
Dennis Premoli
el 24 de Oct. de 2023
Walter Roberson
el 24 de Oct. de 2023
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()
fun('first')
fun('first', 'second')
fun('first', 'second', 'third') %expect error due to too many parameters
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.
Respuestas (1)
Mario Malic
el 24 de Oct. de 2023
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 comentarios
Voss
el 24 de Oct. de 2023
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
Mario Malic
el 24 de Oct. de 2023
Editada: Mario Malic
el 24 de Oct. de 2023
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')
plot(ax, magic(3))
fprintf('after first plot before drawnow\n')
drawnow()
fprintf('after first drawnow, before second plot\n')
plot(ax, 1:5, rand(1,5)*10) %implicitly changes y limit
fprintf('after second plot, before second drawnow\n')
drawnow()
fprintf('after second drawnow\n')
function listenerfun(src, evt)
fprintf('listener here!\n')
disp(evt)
end
Walter Roberson
el 24 de Oct. de 2023
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.
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


