Break a while loop in a GUI Pushbutton

20 ビュー (過去 30 日間)
MINKYUNG KIM
MINKYUNG KIM 2019 年 10 月 14 日
コメント済み: Robert U 2019 年 10 月 14 日
Hi there,
I have a GUI set up and it has a Start pushbutton and a Stop pushbutton. Upon pushing the start, data is transmitted and received between AVR and matlab with Serial communication.
What I am trying to do is while the data is communicated and being run, if the stop button is pressed, the while operation should stop. This should be accomplished if I can just break the while loop, but the code that I've tried using 'assignin' and 'evalin' hasn't succeeded. Actually it occasionally works but sometimes doesn't work. Here is what my code :
function StartButton_Callback(hObject, eventdata, handles)
flag = 0;
assignin('base','flag', flag);
bt = Bluetooth('JCNET-BT-7827',1);
fopen(bt);
while 1
%%Receiving and Display Serial Communication
flag = evalin('base', 'flag');
if flag == 1
break;
end
end
When the EndButton is pressed,
% --- Executes on button press in EndButton.
function EndButton_Callback(hObject, eventdata, handles)
flag = 1;
assignin('base','flag', flag);
Could anyone give me for advices? Thanks a lot in advance!

採用された回答

Robert U
Robert U 2019 年 10 月 14 日
below you find an example for breaking a while loop within a pushbutton controlled GUI. The data exchange is done using guidata. Avoid assignin and evalin if possible. These methods are silly to debug and prone to errors.
function GuiBreak
GUI.fh = figure;
GUI.h1 = uicontrol('style','Edit',...
'string','XX',...
'Units','normalized',...
'Position',[0.1 0.1 0.8 0.2],...
'backgroundcolor','c',...
'Tag','EditField2',...
'Enable','off');
GUI.h2 = uicontrol('Style','PushButton',...
'String','Start',...
'Units','normalized',...
'Position',[0.1 0.4 0.3 0.2],...
'callback',{@func_compute},...
'Tag','StartButton',...
'backgroundcolor',...
'g','FontSize',12);
GUI.h3 = uicontrol('Style','PushButton',...
'String','Stop',...
'Units','normalized',...
'Position',[0.5 0.4 0.3 0.2],...
'callback',{@breakOP},...
'Tag','StopButton',...
'backgroundcolor',...
'r','FontSize',12);
myHandle = guihandles(GUI.fh); % save gui handles to struct
myHandle.breakOP = false; % flag for break OP
guidata(GUI.fh,myHandle); % save structure
end
function func_compute(~,~)
a = 1;
while 1
myHandle = guidata(gcbo); % get structure
myHandle.EditField2.String = num2str(a); % edit field string
pause(0.01);
if myHandle.breakOP % check for break OP flag
break;
end
a = a +1;
end
end
function breakOP(~,~)
myHandle = guidata(gcbo); % get structure
myHandle.breakOP = true; % change break OP flag
guidata(gcbo,myHandle); % save structure
end
Kind regards,
Robert
  1 件のコメント
Robert U
Robert U 2019 年 10 月 14 日
If you want to have continous operation you can provide a flag reset to the start button callback function:
function GuiBreak
GUI.fh = figure;
GUI.h1 = uicontrol('style','Edit',...
'string','XX',...
'Units','normalized',...
'Position',[0.1 0.1 0.8 0.2],...
'backgroundcolor','c',...
'Tag','EditField2',...
'Enable','off');
GUI.h2 = uicontrol('Style','PushButton',...
'String','Start',...
'Units','normalized',...
'Position',[0.1 0.4 0.3 0.2],...
'callback',{@func_compute},...
'Tag','StartButton',...
'backgroundcolor',...
'g','FontSize',12);
GUI.h3 = uicontrol('Style','PushButton',...
'String','Stop',...
'Units','normalized',...
'Position',[0.5 0.4 0.3 0.2],...
'callback',{@breakOP},...
'Tag','StopButton',...
'backgroundcolor',...
'r','FontSize',12);
myHandle = guihandles(GUI.fh); % save gui handles to struct
myHandle.breakOP = false; % flag for break OP
guidata(GUI.fh,myHandle); % save structure
end
function func_compute(~,~)
myHandle = guidata(gcbo);
myHandle.breakOP = false; % flag for break OP
guidata(gcbo,myHandle); % save structure
if ~isempty(str2num(myHandle.EditField2.String))
a = str2num(myHandle.EditField2.String);
else
a = 1;
end
while 1
myHandle = guidata(gcbo); % get structure
myHandle.EditField2.String = num2str(a); % edit field string
pause(0.01);
if myHandle.breakOP % check for break OP flag
break;
end
a = a +1;
end
end
function breakOP(~,~)
myHandle = guidata(gcbo); % get structure
myHandle.breakOP = true; % change break OP flag
guidata(gcbo,myHandle); % save structure
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by