(1) syntax
T = Timer
T = timer ('propertyname1', propertyvalue1, 'propertyname2', propertyvalue2 ,...)
(2) combined with GUI instances
Open guide, add a static text box, set properties, Tag, etc. Set string to null and save:
Add the timer code to the corresponding M file:
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 ,...) callthe local % function named callback in guide_time.m with the given input arguments. % guide_time ('properties', '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 editgui_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}); endif 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_timehandles.output = hobject; handles. ht = timer; % defines a timer and adds it to the handles struct to facilitate the subsequent use of set (handles. HT, 'executionmode', 'fixedrate'); % set (handles. HT, 'period ', 1); % cycle set (handles. HT, 'timerfcn', {@ dispnow, handles}); % start (handles. HT); % start the timer, corresponding stop (handles. HT) % update handles structureguidata (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 structurevarargout {1} = handles. output; % The timer's execution function dispnow (hobject, eventdata, handles) Set (handles. time_disp, 'string', datestr (now); % set the text in the static text box to the current time
Program result: (dynamic display time)
(3) partial attribute Introduction
Executionmode:
'Singleshot ': It can be executed only once. It can be executed multiple times in other modes.
'Fixedspacing': The interval between the last execution time and the next time the queue is added is a specified fixed length of time.
'Fixeddelay': The interval between the last execution time and the next time the queue is added is a specified fixed length of time.
'Fixedrate': the interval between two times added to the execution statement queue is a specified fixed length.
Time interval specified by period: the unit of seconds. Generally, the minimum value is millisecond. The default value is 1 (seconds)
Startdelay startup latency: the latency from the call start (T1) to the first time the execution of timerfcn is added to the execution statement queue of MATLAB. The default value is 0 (seconds)
The number of times taskstoexecute timerfcn will be executed. The default value is 1 (times)
Functions executed by timerfcn
Other attributes (help document ):
Property name |
Property description |
Datatypes, values, and defaults |
AveragePeriod
|
The average timeTimerFcn Executions since the timer started. Note: value isNaN Until timer executes two timer callbacks. |
Datatype: double
Default:NaN
Readonly: Always |
BusyMode
|
Action taken when a timer has to executeTimerFcn Before the completion of previous executionTimerFcn .
‘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 beforeStopFcn . SeeCreating timer callback FunctionsFor more information. |
Datatype: text string, Function Handle, or cell array. Default: Readonly: Never |
ExecutionMode
|
Determines how the timer object schedules timer events. SeeTimer execution modesFor more information. |
Datatype: enumerated string Values: ‘singleShot‘ ‘fixedSpacing‘ ‘fixedDelay‘ ‘fixedRate‘ Default: ‘singleShot‘ Readonly: WhenRunning=‘on‘
|
InstantPeriod
|
The time between the last two executionsTimerFcn . |
Datatype:double
Default:NaN
Readonly:Always
|
Name
|
User-supplied name |
Datatype: text string Default:‘timer- i ‘ , Wherei Is a number indicatingi Th timer object created this session. Note: If you issueclear classes Command, the timer object resetsi To 1. Readonly: Never |
Period
|
Specifies the delay, in seconds, between executionsTimerFcn . |
Datatype:double
Value: Any number: <0.001 Default: 1.0 Readonly: WhenRunning =‘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: WhenRunning =‘on‘
|
StartFcn
|
Function the timer callwhen it starts. SeeCreating timer callback FunctionsFor more information. |
Datatype: text string, Function Handle, or CELL ARRAY Default: Readonly: Never |
StopFcn
|
Function the timer callwhen it stops. The timer stops when:
- You call the timer
stop Function
- When the timer finishes executing
TimerFcn , I. e., the valueTasksExecuted Reaches the limit set byTasksToExecute .
- An error occurs (
ErrorFcn Is called first, followed byStopFcn .)
SeeCreating timer callback FunctionsFor more information. |
Datatype: text string, Function Handle, or cell array. Default: Readonly: Never |
Tag
|
User supplied label |
Datatype: text string Default:‘‘ (Empty string) |
TasksToExecute
|
Specifies the number of times the timer shocould execute the function specified inTimerFcn Property. |
Datatype:double
Value: Any number <0 Default: 1 Readonly: Never |
TasksExecuted
|
The number of times the timer has executedTimerFcn Since the timer was started |
Datatype:double Value: Any number <= 0 Default: 0 Readonly: Always |
TimerFcn
|
Timer callback function. SeeCreating timer callback FunctionsFor more information. |
Datatype: text string, Function Handle, or cell array. Default: Readonly: Never |
Type
|
Identifies the object type |
Datatype: text string Value:‘timer‘
Readonly: Always |
UserData
|
User-supplied data |
Datatype: User-Defined Default:[]
Readonly: Never |
Matlab learning --------- GUI time display, Timer