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

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일
편집: 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

Hello! Thank you for your quick answers (the three of you). This is the answer that fits best que aesthetic requirement (it has to be a push button).
Sadly, I don't seem capable of getting the handler of pushbutton (i thought it would be handles.pushbutton4, but It raises an error telling me that that is no known function or variable)
I am sorry, I am very new to GUIDE, and I am finding not very much intuitive. Thank you again for your time.
There are many different ways to do that, but one simple way to get you started would be to add a tag to the pushbutton object so that later it can be easily found from within your code.
For example, in guide set the "tag" property of your pushbutton to:
'StopPushButton'
and then in the program.m code simply add at the beginning:
handlepushbutton = findobj(0,'tag','StopPushButton');
Thank you very very much, that was exactly what I wanted. Now it seems to work. I say it seems because I dont see the interface, since all is grey now, without buttons or text boxes I had. But the solution you provided doesn't raise an error, so I expect it will work. When I figure out why is all grey. Sigh.
Thank you very much, again
I guess the key thing I didn't have was the drawnow. I didn't know that it allows the pushbutton code to get executed.
I attach a small demo.
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!
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');

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by