startupFcn with a Timer

3 views (last 30 days)
Chloe Soriano
Chloe Soriano on 13 Oct 2022
Edited: Nivedita on 16 Nov 2023
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 Comment
Allen
Allen on 19 Oct 2022
@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.

Sign in to comment.

Answers (1)

Nivedita
Nivedita on 16 Nov 2023
Edited: Nivedita on 16 Nov 2023
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.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!