Interrupt running function with event in a GUI

8 vues (au cours des 30 derniers jours)
Mark T
Mark T le 31 Jan 2016
Hi, I want to use a GUI pushbutton to signal an event to a running function - in particular, to issue the 'keyboard' command in a misfit function which is being called by fminsearch (to investigate the state of the fitting process). Currently I use the following: Before calling fminsearch, main script opens a small GUI with a pushbutton with callback:
function pushbutton1_Callback(hObject, eventdata, handles)
global interrupt
interrupt=true;
...and in the misfit function:
global interrupt
%...
if interrupt, keyboard, interrupt=false; end
This works, but I have to hammer the pushbutton repeatedly to get a response (waiting after one push doesn't seem to work). There is already a drawnow command in the misfit function on each call, which apparently should allow Matlab to process outstanding events, but this does not seem to improve matters (neither does a pause(0.01)).
Can someone suggest a good way to do this which might be more responsive?
  2 commentaires
Geoff Hayes
Geoff Hayes le 31 Jan 2016
Mark - what else is the misfit function doing (is this something that you have written)? Perhaps it is "busy" and once it finishes evaluating whatever it is doing, then it checks the interrupt flag. Please post the code for this function (or an outline of it) so that we can get a better idea of what may be causing the delay.
Mark T
Mark T le 1 Fév 2016
Hello, many thanks for your help. The behavior is not only when using fminsearch and my misfit function (v7.5.0 on Windows 7 x64 I should have mentioned). So, to nail it down, here is a complete stand-alone example:
function test_interrupt1b()
% Test a GUI button for interrupting another sub-routine
% with a keyboard command via "cheap" global variable
% Subfunctions defined below
hf=interrupt_button_sub1() % Create button
subroutine_with_interrupt1b()
delete(hf)
end
function subroutine_with_interrupt1b()
global interrupt
for loop=1:100 % any kind of loop
x=1:10; y=rand(size(x));
figure(2), clf, plot(x,y)
% drawnow % Same problem
pause(0.1) % Nice long pause
if interrupt, keyboard, interrupt=0; end
end
end
function hf=interrupt_button_sub1()
hf=figure;
w=0.15; h=0.1; pos=[(1-w)/2 (1-h)/2 w h];
set(hf,'units','normalized','position',pos,'menubar','none');
hpb=uicontrol('Parent', hf, 'style', 'pushbutton', ...
'String','Interrupt','units','normalized', ...
'position',[0 0 1 1],'fontsize',18);
set(hpb,'callback',@pushbutton_callback)
end
function pushbutton_callback(hObject,callbackdata)
global interrupt, interrupt=1;
end
I hope that runs without any bugs on your computer. Do you also have the response problem? A single click-and-wait doesn't seem to do anything, as though the action/callback is ignored (not just queued). Many thanks for any advice. I can well imagine there is a recommended way to do this. Please keep in mind that in general, the subroutines will be separate functions. MT

Connectez-vous pour commenter.

Réponses (2)

Abdelrhman Morsy
Abdelrhman Morsy le 26 Oct 2019
X=0

Abdelrhman Morsy
Abdelrhman Morsy le 26 Oct 2019
X=10
Y=3
U=X+Y

Catégories

En savoir plus sur Desktop dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by