Stop the execution of a function with a push button (GUIDE)

10 次查看(过去 30 天)
Hiya!
I have a program, which analyzes a series of images extracting data from them. Now, I have been asked to make a gui in order to be more easily used. I have gotten stuck in the part where I call the program itself from the gui (which, by the moment, is a separate program (m-file)).
The calling of the program is easy, and it works. But, the proccess is long, and sometimes the user wants to stop it, in order to change configuration, for example. I can't do the cancelling part. Here is a scheme of what I've got, and what do I want
[GUIDE]
button -> Calls program.m (and it runs well)
button2 -> Stops program.m (this doesn't work) by changing the 'closing' variable to 1
[program.m]
while i< number of images if 'closing' == 1 -> exit program if not -> analyze the i image
Thank you in advance for your time and your response

采纳的回答

Alfonso Nieto-Castanon
编辑:Alfonso Nieto-Castanon 2015-5-24
Using drawnow within program.m processing loop flushes the event queue so your pushbutton callback gets evaluated (otherwise the pushbutton callback will not be evaluated until program.m finishes its computations).
Of course, in addition to this, you need to make sure that your closing variable accessed from within program.m and from the pushbutton callback function are actually the same variable in the same workspaceworkspace. One simple way to do this would be something like:
in guide set pushbutton callback to:
'set(gcbo,''userdata'',1)'
and in program.m use something like:
set(handlepushbutton,'userdata',0);
i=1;
while i< numberofimages
% your iterative computation here
drawnow
if get(handlepushbutton,'userdata') % stop condition
break;
end
i=i+1;
end
  6 个评论
Lednion Bazar
Lednion Bazar 2015-5-25
编辑:Lednion Bazar 2015-5-25
Silly me, I have commented the clf line, and now the interface works perfectly. Still, the cancelling action doesn't work. When I press the button, I see in the command line that the action -set(gcbo,'userdata',1) is executed, but the program won't stop...
PD: The demo works like a charm, by the way PDD: It seems that actually, the value of the "closing" thing doesn't change, it remains 0 even if I press the button
PDDD: Nevermind, it works now. It was something related to miscommunications between the GUIDE interface and the coded part. My bad. Really appreciated the help!
Alfonso Nieto-Castanon
编辑:Alfonso Nieto-Castanon 2015-5-25
if the cancelling action does not work and you are seeing in the command window the line set(gcbo,'userdata') being displayed that probably means that you made your pushbutton callback to simply display that line instead of executing it. To have your callback execute that line you can:
a) edit the properties of your pushbutton and set the callback property to the string
'set(gcbo,''userdata'',1)'
(with the single quotes as displayed above; when the callback property of an uicontrol object is a string, matlab will evaluate that string when a callback action is generated)
or b) edit the properties of your pushbutton and set the callback property to the function handle
@btnStop_Callback
and then within the body of the function btnStop_Callback type the line
set(gcbo,'userdata',1);
(as displayed here, i.e. without the initial quotes or the double single quotes of the other example; when the callback property of an uicontrol object is a function handle, as guide does by default, matlab will invoke the callback function in this handle when a callback action is generated)
My guess is that you might have left the pushbutton callback property set to a function handle as in case (b) above, but then within the body of that function you included the string 'set(gcbo,''userdata'',1)' (as in case (a) above), so Matlab ends up simply displaying that line in the command window when a callback action is generated...

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2015-5-24
I have not found a way to do it with a pushbutton. Using it to set a flag, like "finishNow", that you can check in your intensive loop, does not seem to work. The only way I can get something like that to work is to have a checkbox. So you do something like
% Clear flag and make it visible.
set(handles.chkFinishNow, 'Value', 0);
set(handles.chkFinishNow, 'Visible', 'on');
% Now the intensive loop
for k = 1 : 1000000
% Heavy duty code.
% At and of loop, see if user wants to quit
if get(handles.chkFinishNow, 'Value')
% User checked the box
break; % exit the loop
end
end
% Clear flag and make it invisible.
set(handles.chkFinishNow, 'Value', 0);
set(handles.chkFinishNow, 'Visible', 'off');

Walter Roberson
Walter Roberson 2015-5-24

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by