필터 지우기
필터 지우기

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

조회 수: 28 (최근 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 CenterFile Exchange에서 Interactive 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