Pause until a figure close OR a button is pressed

조회 수: 167 (최근 30일)
Luca Amerio
Luca Amerio 2016년 11월 15일
댓글: Mathias Bannwart 2018년 1월 18일
Hi.
In the middle of my script I ask the user to zoom a figure and call ginput afterwards. I would like to interrupt (return) the script if the user closes the figure. Right now I did
h=figure;
plot(1:1000,randn(1000,1))
zoom on
pause
if ~isvalid(h),return,end
try
[x,y]=ginput(1)
end
However this waits for the user to click a button even if the figure is closed. That is, the user closes the figure, but the script does not notice it until it ALSO press a button. The uiwait or waitfor command could understand if the figure is closed, but then I have to close the figure in order to make the script continue.
Is it possible to obtain a result similar to
figure
zoom on
event=pauseOrWaitfor
if event=='figureClosed'
return
elseif event=='userClickedAButton'
%Do other things here
end
thank you

답변 (1개)

Jan
Jan 2016년 11월 15일
편집: Jan 2016년 11월 15일
h = figure;
drawnow;
disp(clock)
waitfor(h)
disp(clock)
  댓글 수: 2
Luca Amerio
Luca Amerio 2016년 11월 15일
편집: Luca Amerio 2016년 11월 15일
Sorry Jan, but doesn't this require h to be closed to continue?
As I said in the question: "The uiwait or waitfor command could understand if the figure is closed, but then I have to close the figure in order to make the script continue"
Am I wrong?
I'm looking for a solution that continue either if the figure is close or if the user push a button, and maybe recognize which of the two happened.
Mathias Bannwart
Mathias Bannwart 2018년 1월 18일
If both options are needed you could use waitforbuttonpress to detect if the user presses a key or clicks a mouse button and catch closing of the figure in a try/catch statement:
h = figure;
drawnow;
disp(clock)
try
waitforbuttonpress
% Close figure or leave it open
close(h)
disp('mouse or key pressed')
catch
disp('figure closed')
end
disp(clock)
Note that focus on the current figure is required (from R2014 on) to resume program execution after mouse or button presses.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by