How to interrupt the callback function in GUI?

4 views (last 30 days)
Jean cheung
Jean cheung on 27 Mar 2021
Edited: Jean cheung on 30 Mar 2021
In GUI programming, function btn_callback is the callback of uibuttongroup (a group of button A,B and C).
Abilities of A,B and C are start, pause and stop this function, and all of them are in the same callback.
Function btn_callback will run when click button A, but this function will pause and stop when button B and button C is clicked respectively.
When button B was clicked, the function will run from the moment that it paused if button A clicked again.
But when button C was clicked, the function will run from the beginning if button A clicked again.
  2 Comments
Jean cheung
Jean cheung on 30 Mar 2021
Edited: Jean cheung on 30 Mar 2021
function debugGUI
clear;% clear variable
clc;
close all
format compact;% display format style is compact
global SimStep;
% global run;%used in callback function to stop it.
%% Create a GUI
GUI = uifigure('Name','Traffic Simulation');
GUI.Position(3:4) = [800 600];
GUI.WindowState = 'normal';
GUI.Color = [1,1,1];
GUI.CloseRequestFcn = @(GUI, event)my_closereq(GUI);
%% Sim Panel: Simulation Parameter
SimStep = 3600;
pnlCtrl=uipanel(GUI);
pnlCtrl.Position = [650 410 140 180];
pnlCtrl.BackgroundColor = [1,1,1];
lblCtrl = uilabel(pnlCtrl);
lblCtrl.Position = [10 155 120 30];
lblCtrl.HorizontalAlignment = 'center';
lblCtrl.FontWeight = 'bold';
lblCtrl.Text = 'Simulation Control';
bgSimCtrl = uibuttongroup(pnlCtrl);
bgSimCtrl.Position = [10 10 120 80];
bgSimCtrl.BackgroundColor = [1,1,1];
bgSimCtrl.BorderType = 'none';
bgSimCtrl.Interruptible = 'on';
bgSimCtrl.SelectionChangedFcn = @bgSimCtrl_SelectChanged;
btnStart = uitogglebutton(bgSimCtrl);
btnStart.Position = [30 55 60 20];
btnStart.Text = 'Start';
btnPause = uitogglebutton(bgSimCtrl);
btnPause.Position = [30 30 60 20];
btnPause.Text = 'Pause';
btnPause.Value = 1;
btnStop = uitogglebutton(bgSimCtrl);
btnStop.Position = [30 5 60 20];
btnStop.Text = 'Stop';
%% callback
function my_closereq(GUI)
selection = questdlg('Close the window?',...
'Confirmation',...
'Yes','No','Yes');
switch selection
case 'Yes'
delete(GUI)
case 'No'
return
end
end
function bgSimCtrl_SelectChanged(hObject,eventdata,handles)
mode = eventdata.NewValue.Text;
switch mode
case 'Start'
for time = 1:SimStep
pause(1);
disp(num2str(time));
end%time
case 'Pause'
disp('Pause');
case 'Stop'
disp('Stop');
end%switch
end%bgSimCtrl_SelectChanged
end

Sign in to comment.

Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!