How to create two timers in two panels within a UIFigure in MATLAB App Designer?

4 visualizaciones (últimos 30 días)
Good day.
May I ask how to design two timers in two separate panels (that means one panel has one timer) within the same UIFigure in MATLAB App Designer?
I have tried adding two buttons and two labels using the same command with only different values to make the timer. However, it didn't work as only one label was counting the timer.
This is the command for the first and second timer in two different buttons. How should I modify it to have two working timers?
t = timer;
t.Period = 1;
t.ExecutionMode = 'FixedRate';
t.TasksToExecute = 30;
t.TimerFcn = @(~,thisEvent) toggleButton(app);
start(t);
t = timer;
t.Period = 1;
t.ExecutionMode = 'FixedRate';
t.TasksToExecute = 60;
t.TimerFcn = @(~,thisEvent) toggleButton(app);
start(t);

Respuestas (1)

Sourabh Kondapaka
Sourabh Kondapaka el 12 de Ag. de 2020
Hi,
You will need to create 2 different timer objects, say, t1 and t2 and start them individually on corresponding button click.
You may find below code useful.
I have declared 2 timers t1 and t2, which get created and started on button Click of buttons timerButton1 and timerButton2 respectively.
I have also created 2 helper functions i.e. updateLabel1 and updateLabel2 which will update the text in the corresponding labels.
properties (Access = public)
t1; % First timer object.
t2; % Second timer object.
end
methods (Access = public)
function updateLabel1(app, obj, event)
app.Label.Text = string(datetime);
end
function updateLabel2(app, obj, event)
app.Label_2.Text = string(datetime);
end
end
function TimerButton1Pushed(app, event)
app.t1 = timer;
app.t1.Period = 1;
app.t1.ExecutionMode = 'FixedRate';
app.t1.TasksToExecute = 30;
app.t1.TimerFcn = @app.updateLabel1;
start(app.t1);
end
% Button pushed function: TimerButton2
function TimerButton2Pushed(app, event)
app.t2 = timer;
app.t2.Period = 1;
app.t2.ExecutionMode = 'FixedRate';
app.t2.TasksToExecute = 60;
app.t2.TimerFcn = @app.updateLabel2;
start(app.t2);
end

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!

Translated by