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

11 views (last 30 days)
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

Accepted Answer

Rik
Rik on 14 Apr 2017
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 Comment
Rik
Rik on 17 Apr 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!