EventListener for plot's YLim property

3 ビュー (過去 30 日間)
Dennis Premoli
Dennis Premoli 2023 年 10 月 24 日
編集済み: Dennis Premoli 2023 年 10 月 31 日
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 件のコメント
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
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.
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
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.

サインインしてコメントする。

回答 (1 件)

Mario Malic
Mario Malic 2023 年 10 月 24 日
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 件のコメント
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
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
Walter Roberson
Walter Roberson 2023 年 10 月 24 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by