Making STOP - Button

113 views (last 30 days)
noNickame on 20 Jul 2018
Moved: Rik on 4 Mar 2023
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!
  1 Comment
Antonio De lavalle
Antonio De lavalle on 22 Apr 2021
Moved: Rik on 4 Mar 2023
excuse a doubt I need to make a music note player but they also ask me to incorporate a pause button in the program

Sign in to comment.

Accepted Answer

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.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');;
function start_cb(hObj,~)
while true
function stop_cb(hObj,~);
Tayyaba Abro
Tayyaba Abro on 4 Mar 2023
Hello Dennis, can you please help me to implement your code. Actually I am sending continuous data from realterm to MATLAB. I want MATLAB to stop taking data on pressing "Stop button". After stopping data it should ask to input specific row number. And in last it should display that specific row in the text box. Your code worded perfectly in separate .m file but it is not working in my code as it do not stop taking data. Here is my demo code:
function varargout = showguide(varargin)
% SHOWGUIDE MATLAB code for showguide.fig
% SHOWGUIDE, by itself, creates a new SHOWGUIDE or raises the existing
% singleton*.
% H = SHOWGUIDE returns the handle to a new SHOWGUIDE or the handle to
% the existing singleton*.
% SHOWGUIDE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SHOWGUIDE.M with the given input arguments.
% SHOWGUIDE('Property','Value',...) creates a new SHOWGUIDE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before showguide_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to showguide_OpeningFcn via varargin.
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
% Edit the above text to modify the response to help showguide
% Last Modified by GUIDE v2.5 04-Mar-2023 18:02:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @showguide_OpeningFcn, ...
'gui_OutputFcn', @showguide_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
gui_mainfcn(gui_State, varargin{:});
% End initialization code - DO NOT EDIT
% --- Executes just before showguide is made visible.
function showguide_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to showguide (see VARARGIN)
% Choose default command line output for showguide
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes showguide wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = showguide_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
realterm = actxserver ('realterm.realtermintf')
realterm.port = '5'
s = serial('COM4', 'Baudrate', 57600)
serial_baudrate = s.baudrate
data = ''
pause (15)
while (1)
new_data = fscanf(s)
data = [data, new_data]
realterm_baudrate = realterm.baud
set(handles.text3, 'String', num2str(realterm_baudrate))
serial_baudrate = realterm_baudrate
set(handles.text4, 'String', num2str(serial_baudrate))
arrange_data = textscan(data,'%s','Delimiter',' ') %arrange data in array
extract_data = arrange_data{:} % shows arranged data scanned by textscan
set(handles.text2, 'String', extract_data)
row_data = extract_data(3,:)
set(handles.text5, 'String',row_data)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA);

Sign in to comment.

More Answers (1)

Rik on 20 Jul 2018
I use a uicontrol togglebutton in such cases:
%in your looping code:
if get(handles.togglebutton,'Value')
%in the callback of the button:
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.


Find more on Interactive Control and Callbacks 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!