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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by