⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled2.m

📁 一个用matlab编写的时钟程序
💻 M
字号:
h1=figure('name','时间……',...
         'numbertitle','off',...
         'menubar','none',...
         'units','normalized',...
         'windowstyle','normal',...
         'position',[0.2805 0.3912 0.5375 0.502],...
         'resize','on');
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'top')
II=imread('green.bmp');
image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');
A=linspace(0,6.3,1000);
x1=8*cos(A);
y1=8*sin(A);
x2=7*cos(A);
y2=7*sin(A);
plot(x1,y1,'b','linewidth',1.4); %钟表外圆
hold on
plot(x2,y2,'b','linewidth',3.5); %钟表第二个圆
fill(0.4*cos(A),0.4*sin(A),'r'); %钟表圆形红点
axis off 
axis([-10 10 -10 10])
axis equal
set(gca,'position',[[0.13 0.05 0.775 0.815]]) %指定坐标轴位置
%title(date,'fontsize',36)  %获得当前日期作为坐标轴上标
for k=1:12;
   xk=10*cos(-2*pi/12*k+pi/2);
   yk=10*sin(-2*pi/12*k+pi/2);
   plot([xk/9*8 xk/9*7],[yk/9*8 yk/9*7],'color',[0.3 0.8 0.9])
   h=text(xk,yk,num2str(k),'fontsize',20,'color','k',...%钟表数字(1~12)
                           'fontweight','demi',...
                           'HorizontalAlignment','center');
end

% 计算时针位置
ti=clock
th=-(ti(4)+ti(5)/60+ti(6)/3600)/12*2*pi+pi/2;
xh3=4.0*cos(th);
yh3=4.0*sin(th);
xh2=xh3/2+0.5*cos(th-pi/2);
yh2=yh3/2+0.5*sin(th-pi/2);
xh4=xh3/2-0.5*cos(th-pi/2);
yh4=yh3/2-0.5*sin(th-pi/2);
hh=fill([0 xh2 xh3 xh4 0],[0 yh2 yh3 yh4 0],[0.6 0.5 0.3]);


% 计算分针位置
tm=-(ti(5)+ti(6)/60)/60*2*pi+pi/2;
xm3=6.0*cos(tm);
ym3=6.0*sin(tm);
xm2=xm3/2+0.5*cos(tm-pi/2);
ym2=ym3/2+0.5*sin(tm-pi/2);
xm4=xm3/2-0.5*cos(tm-pi/2);
ym4=ym3/2-0.5*sin(tm-pi/2);
hm=fill([0 xm2 xm3 xm4 0],[0 ym2 ym3 ym4 0],[0.6 0.5 0.3]);
% 计算秒针位置
ts=-(ti(6))/60*2*pi+pi/2;
hs=plot([0 7*cos(ts)],[0 7*sin(ts)],...
   'color',[1 0.5 0],'linewidth',3);
set(gcf,'doublebuffer','on');

k=0;
while k<60;
   ti=clock;
   % 计算时针位置
   th=-(ti(4)+ti(5)/60+ti(6)/3600)/12*2*pi+pi/2;
   xh3=4.0*cos(th);
   yh3=4.0*sin(th);
   xh2=xh3/2+0.5*cos(th-pi/2);
   yh2=yh3/2+0.5*sin(th-pi/2);


   xh4=xh3/2-0.5*cos(th-pi/2);
   yh4=yh3/2-0.5*sin(th-pi/2);
   set(hh,'XData',[0 xh2 xh3 xh4 0],'YData',[0 yh2 yh3 yh4 0]);

   % 计算分针位置
   tm=-(ti(5)+ti(6)/60)/60*2*pi+pi/2;
   xm3=6.0*cos(tm);
   ym3=6.0*sin(tm);
   xm2=xm3/2+0.5*cos(tm-pi/2);
   ym2=ym3/2+0.5*sin(tm-pi/2);
   xm4=xm3/2-0.5*cos(tm-pi/2);
   ym4=ym3/2-0.5*sin(tm-pi/2);
   set(hm,'XData',[0 xm2 xm3 xm4 0],'YData',[0 ym2 ym3 ym4 0])
   % 计算秒针位置
   ts=-(ti(6))/60*2*pi+pi/2;
   set(hs,'XData',[0 7*cos(ts)],'YData',[0 7*sin(ts)])   
   drawnow;

  %以下设置数字显示
      if ti(4)<10    %设置时的显示
          hour=strcat(num2str(0),num2str(ti(4)));
      else hour=num2str(ti(4));
      end  

      if ti(5)<10    %设置分的显示
          mint=strcat(num2str(0),num2str(ti(5)));
      else mint=num2str(ti(5));
      end  
   
     s=fix(ti(6));
     if s<10    %设置秒的显示
          secd=strcat(num2str(0),num2str(s));
     else secd=num2str(s);
     end  
     time=strcat(hour,':',mint,':',secd);
     h=uicontrol(h1,'style','text',...
                    'position',[20 20 150 40],...
                    'background','m',...
                    'string',time,...
                    'fontsize',26,...
                    'fontangle','italic',...
                    'fontweight','demi');
   pause(1);
end
%catch
%end 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -