Making STOP - Button

67 ビュー (過去 30 日間)
noNickame
noNickame 2018 年 7 月 20 日
移動済み: Rik 2023 年 3 月 4 日
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 件のコメント
Antonio De lavalle
Antonio De lavalle 2021 年 4 月 22 日
移動済み: Rik 2023 年 3 月 4 日
excuse a doubt I need to make a music note player but they also ask me to incorporate a pause button in the program

サインインしてコメントする。

採用された回答

Dennis
Dennis 2018 年 7 月 20 日
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
  3 件のコメント
Dennis
Dennis 2018 年 7 月 24 日
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)
Tayyaba Abro
Tayyaba Abro 2023 年 3 月 4 日
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)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% 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});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% 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;
handles.run=1
% 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'
realterm.portopen=1
s = serial('COM4', 'Baudrate', 57600)
fclose(s)
fopen(s)
serial_baudrate = s.baudrate
data = ''
pause (15)
while (1)
handles=guidata(hObject);
if handles.run==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)
else
handles.run=1;
disp('stopped')
row_data = extract_data(3,:)
set(handles.text5, 'String',row_data)
break
end
end
% --- 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)
handles.run=0;
guidata(hObject,handles)

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2018 年 7 月 20 日
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 件のコメント
noNickame
noNickame 2018 年 7 月 23 日
Hi! Thanks for your answer!
I already considered using a toggle-button, but the problem is, that - as a toggle-button should do - the button stays "out". So when I start next time it still ends after one run. This is fine for me, who knows, that I need to reset the button, but it might be a problem for an end-user.
So toggle-buttons don't seem to fit for 100%.
Maybe I understood your suggestion wrong and you meant, that I can use one toggle-button instead of two push-buttons? If yes this should be working fine!
Rik
Rik 2018 年 7 月 23 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by