Tricky GUI stop button problem
3 views (last 30 days)
Show older comments
Hello, I have the following problem. I have a series of functions that take long to compute. I usually work with a driver script, but now I made a GUI. I want to make a STOP button. So far I have:
In the GUI:
function AbortUI_Callback(hObject, eventdata, handles)
set(handles.AbortUI,'Value',1);
guidata(hObject, handles);
function AnalyseUI_Callback(hObject, eventdata, handles) %this is the 'Go' button
set(handles.AbortUI,'Value',0)
function1(X,Y,Z) %might take some time
StopCommand=get(handles.AbortUI,'Value');
if ~StopCommand
function2(X,Y,Z) %might take some time
end
StopCommand=get(handles.AbortUI,'Value');
if ~StopCommand
function3(X,Y,Z) %might take some time
end
So, when the stop button is pressed during the execution of function1, Matlab will still continue and stop before function2. What I need is to stop all computations as soon as the button is pressed. I also need the functions to continue to be useable by command-line, meaning that I do not want to check for GUI variables within the functions, to avoid errors due to non-existent variables.
I am sure there is one elegant solution to that. Any idea?
Thanks in advance
0 Comments
Answers (1)
Geoff Hayes
on 25 Aug 2016
Sebastien - you can try using http://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s as a starting point with the difference being that you have a GUI and one or more running functions. It isn't clear what your functions are doing or why they may take some time, so you may need to further clarify what you are doing. But let's suppose that each of these functions has a for or while loop that you want to break out of if you have received a stop command. As per the example, make sure that you have assigned a unique tag/name to your GUI and that the GUI's HandleVisibility property is set to on. Now in each of our functions, you would do something like
function function1(X,Y,Z)
while someConditionIsTrue
% do some work
h = findobj('Tag','MyGuiTag');
if ~isempty(h)
guiHandles = guidata(h);
if isfield(guiHandles,'AbortUI')
if handles.AbortUI == 1
break;
end
end
end
pause(0.001);
end
The above assumes that you have named/tagged your GUI as MyGuiTag and that there is a field in the handles structure named AbortUI which if true then we break out of the while loop. We need the pause so that your function is interruptible. If it isn't then the code to set the AbortUI field could not execute.
By the way, the code to set this field could be done as
function AbortUI_Callback(hObject, eventdata, handles)
handles.AbortUI = 1;
guidata(hObject, handles);
function AnalyseUI_Callback(hObject, eventdata, handles) %this is the 'Go' button
handles.AbortUI = 0;
guidata(hObject, handles);
Your code
set(handles.AbortUI,'Value',1);
may be fine for your version of MATLAB, but I'm not familiar with that in R2014a.
I realize that we are populating your function code with GUI information, but they should still run independently of the GUI as we are careful in checking to see if the found object is valid (not empty) and the field exists before continuing.
See Also
Categories
Find more on Platform and License in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!