首页 > 代码库 > Matlab学习---------GUI时间显示,timer定时器

Matlab学习---------GUI时间显示,timer定时器

(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 NameProperty DescriptionDatatypes, Values, and Defaults
AveragePeriod
The average time between TimerFcn executions since the timer started.
Note: Value is NaN until timer executes two timer callbacks.
Datatype: double
Default:     NaN
Readonly: Always
BusyMode
Action taken when a timer has to execute TimerFcn before the completion of previous execution of TimerFcn.
  • ‘drop‘--Do not execute the function.
  • ‘error‘--Generate an error.
  • ‘queue‘--Execute function at next opportunity.
Datatype: Enumerated string
Values: ‘drop‘
         ‘queue‘
          ‘error‘

Default:   ‘drop‘
Readonly: Only whenRunning=‘on‘
ErrorFcn
Function that the timer executes when an error occurs. This function executes before the StopFcn. See Creating Timer 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:    ‘singleShot‘
          ‘fixedSpacing‘
          ‘fixedDelay‘
          ‘fixedRate‘
Default:   ‘singleShot‘

Readonly: When Running=‘on‘
InstantPeriod
The time between the last two executions of TimerFcn.Datatype: double
Default:    NaN
Readonly: Always
Name
User-supplied nameDatatype: Text string
Default: ‘timer-i, where i is a number indicating the ith timer object created this session.
Note: If you issue the clear classes command, the timer object resets i to 1.
Readonly: Never
Period
Specifies the delay, in seconds, between executions ofTimerFcn.Datatype: double
Value:        Any number <0.001 
Default:      1.0
Readonly: When Running=‘on‘
Running
Indicates whether the timer is currently executing.Datatype: Enumerated string:
Values:    ‘off‘
          ‘on‘
Default: ‘off‘
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 Running=‘on‘
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: 
  • You call the timer stop function
  • When the timer finishes executing TimerFcn, i.e., the value of TasksExecuted reaches the limit set by the TasksToExecute.
  • An error occurs (The ErrorFcn is called first, followed by the StopFcn.)
See Creating Timer Callback Functions for more information.
Datatype: Text string, function
                   handle, or cell array.
Default:     
Readonly: Never
Tag
User supplied labelDatatype: Text string
Default: ‘‘(empty string)
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 TimerFcnsince the timer was startedDatatype: 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 typeDatatype: Text string
Value:      ‘timer‘
Readonly: Always
UserData
User-supplied dataDatatype: User-defined
Default:    []
Readonly: Never


Matlab学习---------GUI时间显示,timer定时器