startupFcn with a Timer
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Allen
el 19 de Oct. de 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.
Respuestas (1)
Nivedita
el 16 de Nov. de 2023
Editada: Nivedita
el 16 de Nov. de 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:
- 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.
- If the user pushes the Start button without changing the edit field value, the lamp's color changes to red after 5 seconds.
- If the user changes the value in the edit field, then after that certain amount of time, the lamp's color changes to yellow.
- 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.
0 comentarios
Ver también
Categorías
Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!