How to prevent triggering 'SizeChangedFcn' callback too many times?
Ältere Kommentare anzeigen
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
am 18 Nov. 2022
probably this post can help
https://de.mathworks.com/matlabcentral/answers/270022-how-to-prevent-resize-function-called-before-mouse-release
or maybe you could start/reset a timer when the resize is called and proceed only, if at least e.g. 5s are reached
Leone Campos
am 19 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
am 28 Nov. 2022
Antworten (1)
Jan
am 28 Nov. 2022
1 Stimme
4 Kommentare
Bruno Luong
am 28 Nov. 2022
Bearbeitet: Bruno Luong
am 28 Nov. 2022
Hmm I test your code on Windows and the callback is not triggered when I use the mouse to drag on bottom left corner. What callback I should assign?
close all
fig = figure('ButtonDownFcn', @resize)
function resize(FigH, EventData, AxesH)
persistent blockCalls % Reject calling this function again until it is finished
disp('resize is called'); % This line never get called
if any(blockCalls), return, end
blockCalls = true;
doResize = true;
while doResize % Repeat until the figure does not change its size anymore
siz = get(FigH, 'Position');
disp('toto')
pause(1.0); % Of course here are some real calculations
set(AxesH, 'Position', [5, 5, siz(3:4)-10]);
drawnow;
doResize = ~isequal(siz, get(FigH, 'Position'));
end
blockCalls = false; % Allow further calls again
end
@Bruno Luong: See the original thread https://www.mathworks.com/matlabcentral/answers/570829-slow-sizechangedfcn-or-resizefcn
set(FigH, 'ResizeFcn', {@resize, AxesH}); % Same for SizeChangedFcn
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
am 28 Nov. 2022
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!