Two while or for loop in app designer.
7 次查看(过去 30 天)
显示 更早的评论
Hi
I am looking for a way for cancle running loop when a button cliked.
For a example there is a code below.
When I clicked the button1 then cliked the button2, something works2 is on going.
But something works2 is returned, the something works1 is on going.
When button 1 is clicked and button 2 is clicked, I want something works1 to stop and something works2 to start.
How can I do this?
function button1Clicked(app, event)
while(1)
%something works1
if ?
return;
end
end
end
function button2Clicked(app, event)
while(1)
%something works2
if ?
return;
end
end
end
2 个评论
dpb
2022-6-2
You'll need a global status variable for each process that notes if it is/isn't running. Check its status on the button callbacks and take the appropriate action.
Geoff Hayes
2022-6-3
@Jae-Hee Park - or consider using a a timer to perform the repeated actions (instead of a loop) then just stop the timer when the appropriate button is pushed.
回答(1 个)
Sai Teja G
2023-9-6
Hi Jae-Hee,
I understand that you want to use both the buttons as per your requirements.
To achieve the desired functionality in App Designer code, you can use callbacks and a flag variable to control the running loop. Here's an example of how you can implement it:
classdef MyApp < matlab.apps.AppBase
% Properties
properties (Access = private)
RunningFlag = false; % Flag to control the running loop
end
% Callbacks
methods (Access = private)
% Button1 callback
function Button1Pushed(app, ~)
% Stop "something works1" if it is running
if app.RunningFlag
app.RunningFlag = false;
end
% Start "something works2"
app.something_works2();
end
% Button2 callback
function Button2Pushed(app, ~)
% Stop "something works1" if it is running
if app.RunningFlag
app.RunningFlag = false;
end
% Start "something works2"
app.something_works2();
end
% Function for "something works1"
function something_works1(app)
app.RunningFlag = true;
while app.RunningFlag
% Code for "something works1" goes here
disp("Something works1 is ongoing...")
end
end
% Function for "something works2"
function something_works2(app)
% Code for "something works2" goes here
disp("Something works2 is ongoing...")
end
end
% App creation and deletion
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and other components here
% ...
% Create Button1
app.Button1 = uibutton(app.UIFigure, 'push');
app.Button1.ButtonPushedFcn = createCallbackFcn(app, @Button1Pushed, true);
% Create Button2
app.Button2 = uibutton(app.UIFigure, 'push');
app.Button2.ButtonPushedFcn = createCallbackFcn(app, @Button2Pushed, true);
% ...
end
end
end
In this example, we have an AppDesigner app with two buttons, Button1 and Button2. When either button is clicked, the corresponding callback function (Button1Pushed or Button2Pushed) is executed. Inside each callback, we check if ‘something_works1’ is running (app.RunningFlag is true) and stop it by setting app.RunningFlag to false. Then, we proceed to start 'something_works2' by calling the ‘something_works2’ method.
The ‘something_works1’ method represents the loop for "something works1" and is controlled by the app.RunningFlag variable. When app.RunningFlag is true, the loop continues, and when it is false, the loop stops.
Hope this resolves your query!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!