MATLAB Answers

Making STOP - Button

104 views (last 30 days)
noNickame
noNickame on 20 Jul 2018
Answered: Antonio De lavalle on 22 Apr 2021
Hi everybody! I'm new here and have a question about GUI's.
I have a GUI which contains (among other things) two (push)buttons. The first one is a "Start" button and the second one should be some kind of a "Stop" button. By pressing "Start" an infinite loop begins and the program starts reading a certain amount of data from a port and saves it to a file. When it finished reading it waits a few seconds and starts reading again. This happens x times, where x is a predifined value.
Now I want, that pressing the "Stop"-Button forces the program to stop at n < x runs (n depending on when I press the button).
For that purpose I defined a variable "run", which is 1 and gets 0, when "Stop" is pressed. So an exit is possible.
That's the plan. Unfortunatelly that doesn't work. I assume, that the rest of the GUI is "frozen" while the program is stuck in the loop, so pressing "Stop" doesn't change anything. I supposed pressing the button triggers some kind of interrupt, so being stuck in the loop is no problem, but sadly it seems as I am wrong (or I have to enable the interrupt, without knowing how). I already read a related post, where the questioner was advised to use a checkbox instead, but sadly this is no option for my purpose.
I hope some of you can help me? :)
Thank you!

Accepted Answer

Dennis
Dennis on 20 Jul 2018
It is hard to guess why it doesn't work without seeing any actual code. Your GUI is not frozen while your loop is running.
Small working exampel:
handles.fig=figure;
handles.pb1=uicontrol('style','pushbutton','position',[100 100 80 40],'callback',@start_cb,'string','Start');
handles.pb2=uicontrol('style','pushbutton','position',[200 100 80 40],'callback',@stop_cb,'string','Stop');
handles.run=1;
guidata(handles.fig,handles)
function start_cb(hObj,~)
while true
handles=guidata(hObj);
if handles.run==1
disp('running')
pause(1)
else
handles.run=1;
guidata(hObj,handles)
disp('stopped')
break
end
end
end
function stop_cb(hObj,~)
handles.run=0;
guidata(hObj,handles)
end
  2 Comments
Dennis
Dennis on 24 Jul 2018
You can place handles.run in your opening function to pass it along with your other handles.
function varargout = TestGUI(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @TestGUI_OpeningFcn, ...
'gui_OutputFcn', @TestGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
function TestGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.run=1; % this is new
guidata(hObject, handles);
axes(handles.Diagramm2);
cla;
function StartButton_Callback(hObject, eventdata, handles)
if ~isempty(instrfind)
fclose(instrfind)
end
while(true)
handles=guidata(hObject);
if handles.run==1
%Code
end
else
handles.run=1;
guidata(hObj,handles)
disp('stopped')
break
end
function StoppButton_Callback(hObject, eventdata, handles)
handles.run=0;
guidata(hObject,handles)

Sign in to comment.

More Answers (2)

Rik
Rik on 20 Jul 2018
I use a uicontrol togglebutton in such cases:
%in your looping code:
if get(handles.togglebutton,'Value')
break
end
%in the callback of the button:
set(hObject,'Value',1)
  2 Comments
Rik
Rik on 23 Jul 2018
You indeed need to reset the togglebutton value when you start your code. You could use a normal pushbutton instead, but that means that you need to be very careful where you load and reload your guidata.
So at the start of you looping function (before you enter the loop), you'll need to set(handles.togglebutton,'Value',1), and then it should work.
PS you see why I don't use GUIDE: a forest of functions calling each other in a convoluted way. Really difficult to understand where you should make changes.

Sign in to comment.


Antonio De lavalle
Antonio De lavalle on 22 Apr 2021
excuse a doubt I need to make a music note player but they also ask me to incorporate a pause button in the program

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!