How to use a pushbutton in a loop in GUI matlab

11 次查看(过去 30 天)
I create a small game I have used 2 push buttons(P and Q), have used a P push button for game start and after clicking that a "while loop" is started, when run that loop I want to use "if" condition, if I click Q button, How to determine Q button clicked or not when loop is running?
Thank you

回答(1 个)

Luna
Luna 2019-1-9
Hello Kavindu,
I was working on the same topic as you asked above and I found a solution with some research.
I made a demo test GUI with 2 buttons.
When this kind of asynchronous programming needs to be performed(for example two different function running in the parallel universes:) ) it would be useful way to define a timer to periodically check if the event has been triggered or a button pressed, etc...
While loop can not be interrupted until the loop executed fully but timer function can.
classdef testGUI < handle
properties
StartButton
StopButton
MainFig
end
properties (SetAccess = private)
timeCounter
whileStatement
trackingFlag = false;
end
events (ListenAccess = 'public', NotifyAccess = 'protected')
StopButtonPressedEvent
end
methods
% constructor method
function obj = testGUI
% figure and 2 pushbutton definitions
obj.MainFig = figure('Name','TEST','units','normalized');
obj.StopButton = uicontrol('Parent',obj.MainFig,'Style','pushbutton','String','Pause','units','normalized','Position',[0.2 0.2 0.3 0.3],'Callback',@obj.StopButtonCallback);
obj.StartButton = uicontrol('Parent',obj.MainFig,'Style','pushbutton','String','Start','units','normalized','Position',[0.6 0.2 0.3 0.3],'Callback',@obj.StartButtonCallback);
% while statement - will be used in timer function for loop control
obj.whileStatement = true;
% event listener for stop button pressed
addlistener(obj,'StopButtonPressedEvent',@(src,evnt)StopButtonPressedListener(obj,src,evnt));
end
function StopButtonCallback(varargin) % this function triggers when StopButton pressed
obj = varargin{1};
obj.notify('StopButtonPressedEvent'); % Notify will send an event notification to every object that is 'listening'.
end
function StartButtonCallback(obj,source,event)
% When start button pressed a timer is created and started
obj.whileStatement = true;
obj.trackingFlag = false;
obj.timeCounter = timer('BusyMode', 'drop', 'ExecutionMode', 'fixedRate', 'Period', 0.1, 'TimerFcn', @obj.timerFcn);
obj.timeCounter.start(); % this executes Timer function callback
end
function StopButtonPressedListener(obj,source,event) % when obj notified by the event button pressed, this function executed.
disp('stop');
obj.whileStatement = false;
obj.trackingFlag = false;
end
function timerFcn(obj,varargin) % timer runs with its given period like a while loop until stop function executed
if obj.whileStatement
if ~obj.trackingFlag %this checks tracking begins
obj.trackingFlag = true;
disp('tracking started');
end
if obj.trackingFlag
disp('continue')
else
stop(obj.timeCounter);
return
end
else
obj.trackingFlag = false;
stop(obj.timeCounter);
return
end
end
end
end

类别

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