在MATLAB中使用计时器提取系统时间 [英] using timer in MATLAB to extract the system time

查看:253
本文介绍了在MATLAB中使用计时器提取系统时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

!我正在使用MATLAB设计模拟时钟.目前,我的代码只是用手(小时,分钟,秒)显示(或绘制)时钟设计,并且没有刻度.这是我的代码:

!I am using MATLAB to design an Analog Clock. Currently, my code simply displays the (or plots rather) the clock design with the hands (hours, mins, secs) and does not tick. Here is my code:

function raviClock(h,m,s)
drawClockFace;


%TIMER begins-------
t = timer;
t.ExecutionMode = 'FixedSpacing';  %Use one of the repeating modes
t.Period = 1;                      %Fire on 1 second intervals
t.TimerFcn = @timer_setup;           %When fired, call this function
start(t);
set(gcf,'DeleteFcn',@(~,~)stop(t));
end

function timer_setup(varargin)

format shortg;
timenow = clock;
h = timenow(4);
m = timenow(5);
s = timenow(6);

% hour hand
hours= h + m/60 + s/3600;
hourAngle= 90 - hours*(360/12);

% compute coordinates for pointing end of hour hand and draw it
[xhour, yhour]= polar2xy(0.6, hourAngle);
plot([0 xhour], [0 yhour], 'k-','linewidth',7.4)

% minute hand
mins= m + s/60;
minsAngle= 90 - mins*(360/60);

% compute coordinates for pointing end of minute hand and draw it
[xmins, ymins]= polar2xy(0.75, minsAngle);
plot([0 xmins], [0 ymins], 'r-','linewidth',4)


%second's hand
second = s;
secAngle = 90- second*(360/60);

[xsec, ysec]= polar2xy(0.85, secAngle);
plot([0 xsec], [0 ysec], 'm:','linewidth',2)
%end   % while ends
end

%--------------------------------------------------------

function drawClockFace

%close all          
axis([-1.2 1.2 -1.2 1.2])  
axis square equal
hold on            


theta= 0;
for k= 0:59
    [xX,yY]= polar2xy(1.05,theta);
        plot(xX,yY,'k*')

        [x,y]= polar2xy(0.9,theta);
    if ( mod(k,5)==0 )  % hour mark
        plot(x,y,'<')
    else                % minute mark
        plot(x,y,'r*')
    end
    theta= theta + 360/60;
    end
end

%-----------------------------------------------------------------
function [x, y] = polar2xy(r,theta)

rads= theta*pi/180;  
x= r*cos(rads);
y= r*sin(rads);
end

当我最初调用函数时,这只是为HOUR,MINUTE和SECOND参数获取值的静态数据.我尝试在while循环中使用以下内容,但并没有太大帮助

This is simply taking a static data of values for the HOUR, MINUTE and SECOND arguments when i initially call my function. I tried using the following in a while loop but it didn't help much

format shortg
c=clock
clockData = fix(c)
h = clockData(4)
m = clockData(5)
s = clockData(6)

,然后将h,m和s传递给各个小节.我想知道如何使用TIMER对象和回调来提取[hrs mins secs]的信息,以便在时钟滴答时可以实时计算相应的点坐标.

and passing the h, m and s to the respective cuntions. I want to know how I can use the TIMER obkjects and callbacks for extracting the information of [hrs mins secs] so i can compute the respective point co-ordinates in real time as the clock ticks.

推荐答案

我会在这里做几件事.

首先,如果要显示当前时间,则可能实际上不需要传递hms输入.将其添加到函数顶部以自动设置这些变量.

First, you probably don't really need to pass the h, m, s inputs, if you are displaying current time. Add this to the top of your function to auto set these variables.

if nargin == 0
    [~,~,~,h,m,s] = datevec(now);
end

然后,很容易花时间定期调用此函数.像这样的东西.

Then, it is pretty easy to use a time to call this function periodically. Something like this.

t = timer;
t.ExecutionMode = 'FixedSpacing';  %Use one of the repeating modes
t.Period = 1;                      %Fire on 1 second intervals
t.TimerFcn = @(~,~)raviClock;      %When fired, call this function (ignoring 2 inputs)
start(t);                          %GO!

使用docsearch timer可以深入记录计时器对象.但是上面的代码应该可以帮助您入门.

Use docsearch timer for in depth documentation of the timer objects. But the code above should get you started.

要停止计时器,请运行

stop(t);

要在关闭窗口时停止计时器,请将stop命令放入窗口删除回调中:

To stop the timer when the window is closed, put the stop command into the window deletion callback:

set(gcf,'DeleteFcn',@(~,~)stop(t));  %NOte:  Better to explicitly use a figure number, rather than gcf.

这篇关于在MATLAB中使用计时器提取系统时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆