MATLAB Answers

How to use 'CancelRequested' property for a progress bar?

2 views (last 30 days)
Levi Blake
Levi Blake on 10 Jan 2020
Commented: Levi Blake on 13 Jan 2020
I currently have an App Designer program set up to run calculations in batches. While running the batch calculations, I'd like to have a way to stop the process without getting a bunch of errors (which is what happens when I close the app or progress bar window). Using the 'Cancel' button in the dialog box as a way to kick the program out of the loop appears to be the right choice, but the documentation for this feature is too sparse to be of any use. Has anyone gotten this feature to work and willing to share how to code it?
Thanks in advance.

  0 Comments

Sign in to comment.

Accepted Answer

Cameron B
Cameron B on 13 Jan 2020
fig = uifigure;
dlg = uiprogressdlg(fig,'Title','Wait',...
'Message','Starting','Cancelable','on','CancelText','Stop this');
wholeRandNum = 37;
for ii = 1:wholeRandNum
%you can put all your other code here
if isempty(findobj(fig)) == 1
dlg.CancelRequested = 1;
end
if dlg.CancelRequested == 1
disp('You Hit Cancel or Exited')
delete(fig)
return
end
dlg.Value = ii/wholeRandNum;
dlg.Message = sprintf('%12.2f%% complete',ii/wholeRandNum*100);
pause(0.1)
end

  1 Comment

Sign in to comment.

More Answers (1)

Cameron B
Cameron B on 13 Jan 2020
Edited: Cameron B on 13 Jan 2020
progressbar = waitbar(0,'Determining optimum slope','CreateCancelBtn','setappdata(gcbf,''Cancel'',1)');
wholeRandNum = 215;
for ii = 1:wholeRandNum
%you can put all your other code here
if getappdata(progressbar,'Cancel') == 1
delete(progressbar)
disp('You Hit Cancel or Exited')
return
end
pause(0.1)
waitbar(ii/wholeRandNum,progressbar,sprintf('%12.2f%% complete',ii/wholeRandNum*100));
end
delete(progressbar)

Sign in to answer this question.

Products


Release

R2018a