How to close a wiatbar when an error occurs before the delete command?

21 次查看(过去 30 天)
Hello All, In my script I have put a waitbar for intimating the user the current number of the calculation. The code goes like this:
pvh=waitbar(0,'calculating.....','name','LOSS ESTIMATION',...
'createcancelbtn','setappdata(pvh,''cancel_callback'',1)');
setappdata(pvh,'cancel_callback',0);
for count_i1 = 1:lako_count
% If the calculation is cancelled exit the FOR loop.
if getappdata(pvh,'cancel_callback')
errordlg('calculation Stopped by user');
break;
end
waitbar(count_i1/ lako_count,pvh,sprintf('Calculating %d %s %d',count_i1,'of', lako_count)); %progress bar for lako_count calculations
... so on the rest of the code goes and at the end I have delete (pvh); % To delete the waitbar fig at the end. This works fine. But one problem what I saw is, say for example if due to some error at other part of the code, the script stops before delete (pvh)and I will get the error but the waitbar doesnt close unless I enter delete() or close all force in the command window manually. Is there a method where when error occurs at any part of the code the waitbar also can be made to close automatically? I looked into try catch method but the problem I will not know what error occured/interrupted the program. I looked into oncleanup also but couldnt get much .
can someone please guide me on what can be done? thanks in advance

采纳的回答

Rik
Rik 2017-4-14
You can use try and catch to catch any errors and close the waitbar.
try
% all your code
catch ME
delete(pvh);
rethrow(ME) %re-issue the error
end
  1 个评论
Rik
Rik 2017-4-17
If you found this answer useful, please mark it as accepted answer. It will give us both reputation points and will make it easier for other people with the same question to find an answer.
If not, please explain why not, so we can try to find a better answer.

请先登录,再进行评论。

更多回答(0 个)

类别

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