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 Name Property Description Datatypes, 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 name Datatype: 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 label Datatype: 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 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:      ‘timer‘

Readonly: Always

UserData User-supplied data Datatype: User-defined

Default:    []

Readonly: Never

时间: 2024-10-06 02:11:36

Matlab学习---------GUI时间显示,timer定时器的相关文章

Matlab学习----------GUI数据管理

向gui handles结构体中添加新的字段: hbtn=uicontrol('tag','mybtn',... 'style','pushbutton',...%普通的按钮 'callback',{@mybtn_Callback,handles},...%回调函数 'string','用户自定义的按钮',...%按钮上的内容 'units','normalized',... 'position',[0.45 0.5 0.2 0.1]); handles.mybtn=hbtn; handles.

Matlab学习---------GUI键盘响应事件的学习

键盘响应事件的学习主要知识: 实例:创建一个GUI,添加两个输入框,一个按钮,实现输入数据之后点击按钮进行验证,验证成功之后关闭当前界面打开新的界面: (1)创建一个新的GUI界面:(添加组件并修改属性) (2)右键按钮添加点击事件响应函数: (3)右键界面,添加键盘按键响应函数: function varargout = gui_key(varargin) % GUI_KEY MATLAB code for gui_key.fig % GUI_KEY, by itself, creates a

Matlab学习-------GUI鼠标事件响应(鼠标划线实例)

(1)打开GUIDE,添加一个坐标轴并保存 (2)添加鼠标响应事件:鼠标按下事件,鼠标运动事件,鼠标松开事件 (3)对相应事件编写程序 function varargout = guide_m(varargin) % GUIDE_M MATLAB code for guide_m.fig % GUIDE_M, by itself, creates a new GUIDE_M or raises the existing % singleton*. % % H = GUIDE_M returns

matlab学习GUI可调的界面窗口

创建一个GUI界面,在此依然利用GUI_01的窗口来演示 发现它的最大化窗口不可调 在GUI绘制中,工具--->选择GUI选项---->选择第二个成比例 再运行就可以调控大小了 原文地址:https://www.cnblogs.com/fanglijiao/p/10261105.html

C#中WebService 的 Timer定时器过段时间后自动停止运行

我用.net做的一个Timer定时器,定时获取短信并给予回复,但大概过了十几个小时以后,Timer定时器会自动停止,再发送短信就不能收到回复,需要在服务器中重新运行定时器才可以,请教各位! 我是在.net framework中的,有一个Global.asax全局应用程序文件,帖代码:public class Global : System.Web.HttpApplication { double iTimerInterval; System.Timers.Timer timer = new Sy

基于MATLAB的GUI(Graphical User Interface)音频实时显示设计

摘要:本文章的设计主要讲基于matlab的gui音频实时显示设计,此次设计的gui相当于一个简洁的音乐播放器,界面只有”录音“和”播放“两个控件,哈哈,够简洁吧.通过”录音“按钮可以实现声音从电脑的声卡录入,并且实时显示录入声音的时域图形和频域图形:待录音结束,通过”播放“按钮可以播放刚录入的声音,并且一边播放一遍实时显示时域和频域图形.本设计的编码在matlab2013a上亲测,可以实现... 一.首先matlab的gui界面设计 打开matlab—>在命令行执行guide—>出现gui编辑

[转载]Matlab实用小技巧——Matlab学习笔记

1.. Ctrl+C 中断正在执行的操作 如果程序不小心进入死循环,或者计算时间太长,可以在命令窗口中使用Ctrl+c来中断.MATLAB这时可能正疲于应付,响应会有些滞后. 2. figure命令新建一个绘图窗口 figure 可以打开一个空的绘图窗口,接下的绘图命令可以将图画在它里面,而不会覆盖以前的绘图窗口.当有多个figure窗口时,在命令窗口中执行如Plot等命令将覆盖当前figure窗口中的对象.所谓的当前figure窗口,也就是最后一次查看的窗口(可以用命令gcf得到). figu

asp.net中Timer定时器在web中无刷新的使用

最近在做一个项目的时候,web端的数据需要与数据源进行实时同步,并保证数据的准确性,当时,考虑到使用ajax异步刷新技术.但后来在网上查找相关资料时,发现这样做,太浪费资源了,因为ajax的提交请求不应该这么频繁的,只适用于那种手动请求响应的那种,因此这种办法是行不通了,后来,发现asp.net中有一个定时器Timer,可以进行实时同步数据,因此本人就做了一个小小的测试,发现还挺好用的,于是乎就有了下文.如下: aspx页面中的代码: <form id="form1" runat

Matlab学习-----------GUIDE菜单学习

打开GUIDE,添加组件,然后点击菜单编辑按钮: 编辑菜单和子菜单,包含快捷键,label和tag,然后点击View编辑菜单的回调函数: 为按钮添加回调函数,程序如下: function varargout = guide_menu(varargin) % GUIDE_MENU MATLAB code for guide_menu.fig % GUIDE_MENU, by itself, creates a new GUIDE_MENU or raises the existing % sing