startupFcn with a Timer

조회 수: 7 (최근 30일)
Chloe Soriano
Chloe Soriano 2022년 10월 13일
편집: Nivedita 2023년 11월 16일
I am making a GUI in App Designer where the user is able to select the day and time the timer starts and how long they want the timer to run; however, I have all of the properties of the timer set up in the startupFcn, but the user selects how long they want it to run after the timer is already set up. The default for how long the timer runs is 1 hour, and so even if the user selects a different number (say 9 hours), it doesn't run for the amount of time the user selects it to be, only runs for an hour. Any other ways on how I should fix this? I tried putting the timer in other places besides the startupFcn, but errors keep popping up.
  댓글 수: 1
Allen
Allen 2022년 10월 19일
@Chloe Soriano please provide additional details regarding how your app is setup and possibly code from your startupFcn and callback functions for objects related to the user changing the timer duration value.

댓글을 달려면 로그인하십시오.

답변 (1개)

Nivedita
Nivedita 2023년 11월 16일
편집: Nivedita 2023년 11월 16일
Hello Chloe,
I understand that you want to use a timer in your app and let the user determine how long it should be running. I have created a simple app with a Numeric Edit field, a lamp and a push button and incorporated a timer to change the color of the lamp according to the value in the edit field. Here are a few more details regarding the same:
  1. The default value of the edit field has been set to 5. The timer is initialized in the "startup" function of the app and the "StartDelay" property of the timer has been set to this default value.
  2. If the user pushes the Start button without changing the edit field value, the lamp's color changes to red after 5 seconds.
  3. If the user changes the value in the edit field, then after that certain amount of time, the lamp's color changes to yellow.
  4. The changing of the lamp's color has been handled by the "TimerFcn" callback of the timer.
Please refer to the code below for the above explanation:
properties (Access = private)
myTimer % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.myTimer = timer;
app.myTimer.StartDelay = app.DurationEditField.Value;
app.myTimer.TimerFcn = @(~,~) set(app.Lamp, 'Color', 'red');
end
% Value changed function: DurationEditField
function DurationEditFieldValueChanged(app, event)
app.myTimer.StartDelay = app.DurationEditField.Value;
app.myTimer.TimerFcn = @(~,~) set(app.Lamp, 'Color', 'yellow');
end
% Button pushed function: StartButton
function StartButtonPushed(app, event)
start(app.myTimer);
end
end
I have also attached the .mlapp file for further reference.
I hope this helps!
Regards,
Nivedita.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by