Clear Filters
Clear Filters

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

16 views (last 30 days)
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 Comments
Jonas
Jonas on 28 Nov 2022
Edited: Jonas on 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 on 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.

Sign in to comment.

Answers (1)

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

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!