How to prevent triggering 'SizeChangedFcn' callback too many times?

23 ビュー (過去 30 日間)
Leone Campos
Leone Campos 2022 年 11 月 18 日
コメント済み: Leone Campos 2022 年 11 月 28 日
When I plot a figure and resize it manually (by clicking and dragging its window) I am triggering the SizeChangedFcn callback too many times.
In my application, it doesn't cause any time processing issues, but I was curious about how to avoid such behaviour if needed.
For my specific case, it would be enough if the event was triggered just when I release the mouse button after resize the figure window.
Here is my MWE to show how many times the event is being triggered:
clc; close all;
figure('SizeChangedFcn',@figureCallback)
function figureCallback(~,~)
disp('ok')
end
  4 件のコメント
Jonas
Jonas 2022 年 11 月 28 日
編集済み: Jonas 2022 年 11 月 28 日
i was more thinking about a timer object. In the follwing example the plot data is changed after 2 seconds after stopping changing the window size
updateDelay=2;
yourTimer=timer("StartDelay",updateDelay,'ExecutionMode','singleShot');
close all;
figure()
plot(rand(5));
ax=gca;
set(yourTimer,"TimerFcn",@(~,~)writeDataToWindow(ax));
set(gcf,'SizeChangedFcn',@(~,~)figureCallback(yourTimer));
function []=writeDataToWindow(ax)
plot(ax,rand(5));
end
function figureCallback(yourTimer)
% restart timer repeatedly when window size is changed
stop(yourTimer);
start(yourTimer);
end
Leone Campos
Leone Campos 2022 年 11 月 28 日
Thank you @Jonas! That's a pretty elegant solution.
Although the SizeChangedFcn is still being triggered multiple times, this way is much more organized.

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

回答 (1 件)

Jan
Jan 2022 年 11 月 28 日
  4 件のコメント
Bruno Luong
Bruno Luong 2022 年 11 月 28 日
OK Thanks what leads me to the errir is this sentense in the Answer:
"One solution is to ignore the built-in resize methods and use a specific WindowsButtonDownFcn to emulate a resizing"
Leone Campos
Leone Campos 2022 年 11 月 28 日
Thanks for the answer @Jan, but I think that the solution you came up with (using persistent variables) is similar to mine in the comments.
I have also tried using both WindowsButtonDownFcn and WindowButtonUpFcn, but I guess the clicking action (up and down) of resizing is not triggering any of those callbacks.

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by