(1)语法
T = timer
T = timer(‘PropertyName1‘, PropertyValue1, ‘PropertyName2‘, PropertyValue2,...)
(2)结合GUI实例
打开GUIDE,添加静态文本框,设置属性,tag等,将String设置为空,保存:
在相应的m文件中添加定时器代码:
function varargout = guide_time(varargin) % GUIDE_TIME MATLAB code for guide_time.fig % GUIDE_TIME, by itself, creates a new GUIDE_TIME or raises the existing % singleton*. % % H = GUIDE_TIME returns the handle to a new GUIDE_TIME or the handle to % the existing singleton*. % % GUIDE_TIME('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUIDE_TIME.M with the given input arguments. % % GUIDE_TIME('Property','Value',...) creates a new GUIDE_TIME or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before guide_time_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to guide_time_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help guide_time % Last Modified by GUIDE v2.5 24-Aug-2014 08:53:46 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @guide_time_OpeningFcn, ... 'gui_OutputFcn', @guide_time_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before guide_time is made visible. function guide_time_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to guide_time (see VARARGIN) % Choose default command line output for guide_time handles.output = hObject; handles.ht=timer;%定义一个定时器,添加到handles结构体中,方便后面使用 set(handles.ht,'ExecutionMode','FixedRate');%ExecutionMode 执行的模式 set(handles.ht,'Period',1);%周期 set(handles.ht,'TimerFcn',{@dispNow,handles});%定时器的执行函数 start(handles.ht);%启动定时器,对应的stop(handles.ht) % Update handles structure guidata(hObject, handles); % UIWAIT makes guide_time wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = guide_time_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; %定时器的执行函数 function dispNow(hObject,eventdata,handles) set(handles.time_disp,'string',datestr(now));%设置静态文本框中的文本为当前时间
程序结果:(动态显示时间)
(3)部分属性介绍
ExecutionMode 执行的模式:
‘singleShot‘ :只能执行一次,其他模式都可以执行多次
‘fixedSpacing‘ :上一次执行完毕的时刻到下一次被加入队列的时刻之间的间隔是指定的固定时间长度
‘fixedDelay‘ :上一次开始执行的时刻到下一次被加入队列的时刻之间的间隔是指定的固定时间长度
‘fixedRate‘ :两次被加入到执行语句队列的时刻之间的间隔是指定的固定时间长度
Period 指定的时间间隔: 秒为单位,通常最小值为毫秒, 默认为1(秒)
StartDelay 启动时延 : 从调用start(t1),开始到第一次把TimerFcn的执行加入到Matlab的执行语句队列中去的时延, 默认值为0(秒)
TasksToExecute TimerFcn将被执行的次数,默认为1(次)
TimerFcn 执行的函数
其他属性(帮助文档):
Property Name | Property Description | Datatypes, Values, and Defaults |
AveragePeriod
|
The average time between TimerFcn executions since the timer started.
Note: Value is |
Datatype: double
Default: NaN
Readonly: Always |
BusyMode
|
Action taken when a timer has to execute TimerFcn before the completion of previous execution of TimerFcn .
|
Datatype: Enumerated string
Values: ‘queue‘ ‘error‘ Default: Readonly: Only when |
ErrorFcn
|
Function that the timer executes when an error occurs. This function executes before the StopFcn . See CreatingTimer Callback Functions for more information. |
Datatype: Text string, function
handle, or cell array. Default: Readonly: Never |
ExecutionMode
|
Determines how the timer object schedules timer events. See Timer Execution Modes for more information. |
Datatype: Enumerated string
Values: ‘fixedSpacing‘ ‘fixedDelay‘ ‘fixedRate‘ Default: ‘singleShot‘ Readonly: When |
InstantPeriod
|
The time between the last two executions of TimerFcn . |
Datatype: double
Default: NaN
Readonly: Always
|
Name
|
User-supplied name | Datatype: Text string
Default: Note: If you issue the Readonly: Never |
Period
|
Specifies the delay, in seconds, between executions ofTimerFcn . |
Datatype: double
Value: Any number <0.001
Default: 1.0 Readonly: When |
Running
|
Indicates whether the timer is currently executing. | Datatype: Enumerated string:
Values:
Default: Readonly: Always |
StartDelay
|
Specifies the delay, in seconds, between the start of the timer and the first execution of the function specified inTimerFcn . |
Datatype: double
Value: Any number <=0
Default: 0 Readonly: When |
StartFcn
|
Function the timer calls when it starts. See Creating Timer Callback Functions for more information. |
Datatype: Text string, function
handle, or cell array Default: Readonly: Never |
StopFcn
|
Function the timer calls when it stops. The timer stops when:
See Creating |
Datatype: Text string, function
handle, or cell array. Default: Readonly: Never |
Tag
|
User supplied label | Datatype: Text string
Default: |
TasksToExecute
|
Specifies the number of times the timer should execute the function specified in the TimerFcn property. |
Datatype: double
Value: Any number <0
Default: 1 Readonly: Never |
TasksExecuted
|
The number of times the timer has executed TimerFcn since the timer was started |
Datatype: double
Value: Any number <=0 Default: 0 Readonly: Always |
TimerFcn
|
Timer callback function. See Creating Timer Callback Functions for more information. |
Datatype: Text string, function
handle, or cell array. Default: Readonly: Never |
Type
|
Identifies the object type | Datatype: Text string
Value: Readonly: Always |
UserData
|
User-supplied data | Datatype: User-defined
Default: Readonly: Never |