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

27 Ansichten (letzte 30 Tage)
Leone Campos
Leone Campos am 18 Nov. 2022
Kommentiert: Leone Campos am 28 Nov. 2022
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 Kommentare
Jonas
Jonas am 28 Nov. 2022
Bearbeitet: Jonas am 28 Nov. 2022
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 am 28 Nov. 2022
Thank you @Jonas! That's a pretty elegant solution.
Although the SizeChangedFcn is still being triggered multiple times, this way is much more organized.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 28 Nov. 2022
  4 Kommentare
Bruno Luong
Bruno Luong am 28 Nov. 2022
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 am 28 Nov. 2022
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by