!我正在使用 MATLAB 设计一个模拟时钟。目前,我的代码只是用指针(小时、分钟、秒)显示(或者更确切地说是绘图)时钟设计并且不会打勾。这是我的代码:
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循环中使用以下内容,但没有太大帮助
format shortg
c=clock
clockData = fix(c)
h = clockData(4)
m = clockData(5)
s = clockData(6)
并将 h、m 和 s 传递给相应的参数。我想知道如何使用 TIMER 对象和回调来提取 [hrs mins secs] 的信息,这样我就可以在时钟滴答时实时计算各个点坐标。