Aborting callback execution in GUI - Drawnow error

2 ビュー (過去 30 日間)
Bart Boesman
Bart Boesman 2012 年 12 月 14 日
Hi all,
I'm working on a GUI, where I have one task that is very computationally intensive. Therefore I want to add a button that enables the user to cancel the current calculation. I read a lot of posts that tackled the same problem, and therefore I implemented a try catch statement. Pressing on the cancel button creates an error in its own callback function and I try to catch this error in the other callback which contains my computation. This compution consists of a loop that runs over different frequencies, thus in the catch block, I use the break function to get out of this loop. Inside the loop I also place a few drawnow commands to be able to see when the cancel event is triggered. When pressing my cancel button during a computation, I see the error that I generate, but matlab also generates another error stating: "Error using drawnow" / "Error when evaluating uicontrol Callback". And I have entirely no idea where it comes from. Any hint is welcome ... At the time I get this message, matlab is still in the loop, meaning that the break statement is not reached yet ...?
Thanks!
Bart

採用された回答

Matt Fig
Matt Fig 2012 年 12 月 14 日
I am not sure why you are using errors to try to cancel an operation. It seems simpler to just have the cancel button set a flag that your operation checks. Here is a simple example of what I mean:
function [] = gui_breakloop()
% How to stop a while loop with a toggle button.
S.f = figure('name','gui_breakloop',...
'menubar','none',...
'numbert','off',...
'pos',[100 100 300 150]);
S.pb = uicontrol('Style','toggle',...
'Units','pix',...
'Position',[10 10 130 130],...
'CallBack',@pb_call,...
'String','Start Loop');
S.pb(2) = uicontrol('Style','toggle',...
'Units','pix',...
'Position',[160 10 130 130],...
'CallBack',@pb_call2,...
'String','Cancel',...
'enable','off',...
'userdata',0);
movegui('center')
guidata(S.f,S); % Store the handles in GUIDATA.
function [] = pb_call(varargin)
% Callback for looping button
S = guidata(gcbf);
set(S.pb(1),'string','Looping!')
set(S.pb(2),'enable','on')
pause(.05)
while 1
A = sort(rand(1000));
pause(.05) % Forces graphics refresh
if get(S.pb(2),'userdata')
set(S.pb(1),'string','No Loop')
set(S.pb(2),'userdata',0)
break
end
end
set(S.pb(2),'enable','off')
function [] = pb_call2(varargin)
S = guidata(gcbf);
set(S.pb(2),'userdata',1)

その他の回答 (1 件)

Bart Boesman
Bart Boesman 2012 年 12 月 14 日
Thanks for this example! This works indeed. I was thinking of using try catch because one iteration takes a long time (due to a set of integrals that has to be computed), such that computation could be interrupted at any time. Now it only breaks the loop after the iteration in which the compution is busy (depending on where I put the break statement ...). But ok, this is an elegant solution, thanks!

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by