Two while or for loop in app designer.

조회 수: 5 (최근 30일)
Jae-Hee Park
Jae-Hee Park 2022년 6월 2일
답변: Sai Teja G 2023년 9월 6일
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
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
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
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!

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by