Waitbar won't close

조회 수: 24 (최근 30일)
Denys
Denys 2011년 5월 26일
댓글: Walter Roberson 2020년 1월 3일
[EDIT: Thu May 26 18:36:12 UTC 2011 - Reformat - MKF]
Hi all,
I'm trying to use a waitbar to make my function a bit more user friendly.
It works as I intend it to, except for the fact that I cannot close the waitbar window after the function is done executing, or if I cancell the process half way through. I actually have to "quit force" to shut matlab down to close get rid of the waitbar window.
Bellow is an example of how I create the waitbar. Could somebody suggest what could be causing the problem?
==================================================
%estimate length of time of computation
%time for first loop:
time1=10;
%time for second loop:
time2=20;
%total time:
total_time=time1+time2;
%create a waitbar
h.waitbar=waitbar(0,'Importing and processing data...','Name','Progress',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
setappdata(h.waitbar,'canceling',0)
%first loop:
for step=1:steps
waitbar(step/steps*time1/total_time)
end
%update waitbar message
waitbar(time1/total_time,h.waitbar,'Generating the animation...');
%second loop:
for step=1:steps
waitbar(time1/total_time+step/steps*time2/total_time,h.waitbar)
end
%close the waitbar
close(h.waitbar);
==================================================

채택된 답변

Matt Fig
Matt Fig 2011년 5월 26일
Use:
delete(h.waitbar);
instead of
close(h.waitbar);
Basically what happens is that when you set the CreatCancelBtn property this also sets the CloseRequestFcn to the same thing. Thus calling CLOSE only sets the appdata to the figure.
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 5월 26일
And remember to use try/catch blocks to detect errors and handle them gracefully.
If you have a quite new version you could use onCleanup() instead.
Bill Otto
Bill Otto 2015년 6월 18일
Thanks Matt Fig for the suggestion below. Just what I needed to clean up after several program aborts.
F = findall(0,'type','figure','tag','TMWWaitbar');
delete(F);

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

추가 답변 (1개)

Giridhar Ramanujam
Giridhar Ramanujam 2020년 1월 3일
You can also delete the waitbar using the following line.
h.waitbar.delete;
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 1월 3일
Yes, if you still have its handle, and you are using R2014b or later (the question was originally asked in 2011)
The findall() approaches are usable in cases where the function stopped because of an error, and the variable holding the handle was automatically destroyed.

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

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by