The timer is powerful, and this article records a simple use case.
Function description
Click to start timing, and call the callback function when the set time is reached.
In the callback function, the value of "numerical display" is incremented by one.
The period of the timer can be modified manually.
UI layout
Among them: The character boxes are all numeric boxes, not text boxes.
component name
Implementation steps
1. Add properties
Here I choose the handle of the private attribute
timer
properties (Access = private)
Timer_id; % 定时器id
end
2. Add timer control function
Here I choose private function
methods (Access = private)
% 定时器初始化,重复模式
function timer_init(app)
app.Timer_id = timer;
app.Timer_id.StartDelay = 0.01; % 开启的延时生效时间
app.Timer_id.Period = 1.0;% 周期
app.Timer_id.ExecutionMode = 'fixedSpacing';
app.Timer_id.TimerFcn = @(~, ~) timer_handler(app);
end
% 定时器启动
function timer_start(app)
start(app.Timer_id);
end
% 定时停止
function timer_stop(app)
stop(app.Timer_id);
end
% 删除定时器
function timer_delete(app)
delete(app.Timer_id);
end
%定时器回调
function timer_handler(app)
%执行定时器任务
app.EditField.Value = app.EditField.Value +1;
end
end
Explanation of some concepts:
- The period timed by the Period
timer. - The interval between when StartDelay
starts the timer and when the countdown starts.
For example: StartDelay = 4, Period = 2
start(app.Timer_id); will wait for 4s of StartDelay first, then count down 2s of Period, and then call the callback function of TimerFcn for processing. - ExecutionMode
specifies the triggering method of the timer (trigger once, or cyclically...) There are four methods in total, which can be selected according to actual needs.
sigleShot; only executed once, the timer will be automatically closed after execution. In other modes, fixedDelay can be executed cyclically; fixedRate
is the interval between the time when the last TimerFcn is executed and the time when the next TimerFcn is added to the queue; the interval between the last execution and the next addition to the queue is fixedSpacing: two times before and after are added to the execution Interval between statement queue moments
- delete and modify period
To delete a timer, you can delete or modify the period only after the timer is stopped.
3. Initialize the timer
Right-click app.UIFigure to add a callback function
% Code that executes after component creation
function startupFcn(app)
timer_init(app);
end
After the software is opened, the initialization of the timer will be performed.
4. Start or stop the timer
Add keypress callback for stop button. (This button uses the status button)
% Value changed function: Button_Contrl
function Button_ContrlValueChanged(app, event)
value = app.Button_Contrl.Value;
if value == 1
app.Button_Contrl.Text = '停止';
timer_start(app);
else
app.Button_Contrl.Text = '开始';
timer_stop(app);
end
end
5. Set the timer period
You can add the callback function of the corresponding button.
Note that you must stop it before you can modify it.
% Button pushed function: Button_SetPeriod
function Button_SetPeriodPushed(app, event)
timer_stop(app);
app.Timer_id.Period = app.EditField_PeriodValue.Value;
timer_start(app);
end
Add a callback function for clearing the value to zero.
% Button pushed function: Button_Clear
function Button_ClearPushed(app, event)
app.EditField_Show.Value = 0;
end
6. Set the software to close the callback
When the software is closed, release the timer
function UIFigureCloseRequest(app, event)
% 停止定时器,然后删除
timer_stop(app);
timer_delete(app);
delete(app);
end
Experimental results
Click the start button
and click clear to clear the data to 0.
Set the timing period to 0.2, and the speed of the value increase is significantly faster.
Click stop, the timer stops, and the value does not increase.