Break a while loop in a GUI Pushbutton

17 views (last 30 days)
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!

Accepted Answer

Robert U
Robert U on 14 Oct 2019
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 Comment
Robert U
Robert U on 14 Oct 2019
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

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names 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!

Translated by