Addlistener in uiprogressDlg for CancelRequested

6 次查看(过去 30 天)
Hey,
I would like to add a listener to the Variable CancelRequested for uiprogressDlg. This shall be possible outside a loop without polling all the time by hand. Since only get.CancelRequested will be asked, which is not observable, I get no respond. I would appreciate your help, how to solve this.
classdef Untitled
properties
fig
dlg
end
methods
function obj = Untitled()
obj.fig = uifigure;
obj.dlg = uiprogressdlg(obj.fig,'Title','Wait',...
'Message','Starting','Cancelable','on','CancelText','Cancel','Indeterminate','on');
addlistener(obj.dlg,"CancelRequested",'PostSet',@obj.cancelMeasurement);
main(obj)
end
function main(obj)
counter = 50;
for ii = 1:counter %you can put all your other code here
%% outcommented is not a solution ;)
% if isempty(findobj(fig)) == 1
% obj.dlg.CancelRequested = 1;
% end
% if dlg.CancelRequested == 1
% disp('You Hit Cancel or Exited')
% delete(fig)
% return
% end
% obj.dlg.Value = ii/counter;
obj.dlg.Message = sprintf('%12.2f%% complete',ii/counter*100);
pause(0.1)
end
obj.dlg.CancelRequested
close(obj.dlg);
end
function cancelMeasurement(obj,~,~)
close(obj.dlg);
close(obj.fig);
errordlg('Measurement aborted by User','Information');
end
end
end

回答(1 个)

Arthi Sathyamurthi
Arthi Sathyamurthi 2021-10-25
Hello Markus,
The code “obj.dlg.CancelRequested” only gets the state of CancelRequested and returns the value. Hence set the state of CancelRequested to a value as
obj.dlg.CancelRequested = 1;
in order to call the addlistener handle.

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by