程序
%% === 主程序时钟程序======
%% === clock1.m (注意使用原始文件名) ======
function [ ] = clock1( )
try
close all
%% === create the figure ============
h0 = figure( 'MenuBar' , 'none' ,...
'Color' , 'white' ,...
'NumberTitle' , 'off' ,...
'Name' , 'Clock' ) ;
%% === 背景程序,不用可以删==========
% ha=axes('units','normalized','position',[0 0 1 1]) ;
% uistack(ha,'down')
% II=imread('3.jpg') ; %=== 3.jpg 是图片地址====
% image(II)
% colormap gray
% set(ha,'handlevisibility','off','visible','off') ;
%% === create a menu to set the timing clock =====
regular_t = uimenu('Parent' , h0 ,...
'Label' , '闹钟' ,...
'Callback' , @timing) ; %=== 调用了timing.m ====
%% === create a menu to calculate the time =====
cal_t = uimenu('Parent' , h0 ,...
'Label' , '计时' ,...
'Callback' , ['run calc_time ;' ]) ; %=== 运行了calc_time.m 文件%% =================
theta = linspace(0 , 6.288 , 1000) ; % 6.28 近似等于2*pi
x1 = 8*cos(theta) ;
y1 = 8*sin(theta) ;
plot(x1 , y1 , 'color' , [0.5 0.5 0.5] , 'linewidth' , 16) %===时钟的外层hold on
axis equal
fill(x1 , y1 , [0.7 0.7 0.7]) ; %=== 时钟的内层
text( 0 , 5 , 'the big bag' , 'fontsize' , 16 , 'color' , [0.8 0.8 0.8] , 'HorizontalAlignment' , 'center') ; % the name of the clock lighting phong
material metal
axis off
axis([-10 10 -10 10])
set(gca , 'position' , [0.1 0.1 0.775 0.815]) ;
%% ===========================
t_title = floor(clock) ;
title([num2str(t_title(1)) '年' num2str(t_title(2)) '月' num2str(t_title(3)) '日'] , 'fontname' , 'Verdana' , 'fontsize' , 13 , 'color' , [0.2 0.2 0.2]) ;
%% === 创建一个edit控件去显示数字时间=========
h0_edit = uicontrol('parent', h0, ... % a edit to show the hour, minute, second dymaically
'enable', 'inactive', ...
'BackgroundColor', [1 0.8 0.4], ...
'fontname' , 'Verdana' ,...
'fontsize' , 12 ,...
'style', 'edit', ...
'horizontal', 'center', ...
'units','normalized' , 'position', [0.38 0.04 0.22 0.08], ...
'string', [num2str(t_title(4)) ' : ' num2str(t_title(5)) ' : ' num2str(t_title(6))]);
for k=1:12
xk=8.8*cos( -2*pi / 12*k + pi/2);
yk=8.8*sin(-2*pi / 12*k + pi/2);
plot([xk/9*8 xk/9*7],[yk/9*8 yk/9*7] , 'color' , [0.8 0.8 0.8] , 'linewidth' , 3) ;
% text(xk , yk , num2str(k) , 'fontsize' , 16 , 'color' , [0.9 0.3 0.8] , 'HorizontalAlignment' , 'center') ; % mark of clock
end
%% ==========================
ti=clock; % get the current time
%% === 时针初始位置===========
th_out = -(ti(4) + ti(5)/60 + ti(6)/3600)/12*2*pi+pi/2 ; % the angle of clock is different from the axes, so pi/2 is needed to correct.
th_in = th_out - pi;
hh = plot([1.5*cos(th_in) 5*cos(th_out)] , [1.5*sin(th_in) 5*sin(th_out)] , 'color' , [0.8 0.8 0.8] , 'linewidth' , 9) ;
%% === 分针初始位置===========
tm_out = -(ti(5) + ti(6)/60)/60*2*pi + pi/2 ;
tm_in = tm_out - pi;
hm = plot([2*cos(tm_in) 6*cos(tm_out)] , [2*sin(tm_in) 6*sin(tm_out)] , 'color' , [0.8 0.8 0.8] , 'linewidth' , 7) ;
%% === 秒针初始位置============
ts_out = -(ti(6)/60*2*pi + pi/2) ;
ts_in = ts_out - pi;
hs = plot([3*cos(ts_in) 7*cos(ts_out)] , [3*sin(ts_in) 7*sin(ts_out)] , 'color' , [0.8 0.8 0.8] , 'linewidth' , 4) ;
fill([0.2*cos(theta)] , [0.2*sin(theta)] , 'w') ; % the center crew of the clock
set(gcf , 'doublebuffer' , 'on') ;
flag = 3 ; % ===
while ishandle(h0); %=== 请不要使用1 来无限循环
ti = floor(clock) ; % get the current time from loop
str = [num2str(ti(4)) ' : ' num2str(ti(5)) ' : ' num2str(ti(6))] ;
set(h0_edit, 'String', str) ; % use the edit to show the current time
%% === calculate the next position of pointer of hour =====
th_out = -(ti(4) + ti(5)/60 + ti(6)/3600)/12*2*pi+pi/2 ;
th_in = th_out - pi;
set(hh , 'XData' , [1.5*cos(th_in) 5*cos(th_out)] , 'YData' , [1.5*sin(th_in) 5*sin(th_out)])
%% === calculate the next position of pointer of minute =====
tm_out = -(ti(5) + ti(6)/60)/60*2*pi + pi/2 ;
tm_in = tm_out - pi;
set(hm , 'XData' , [2*cos(tm_in) 6*cos(tm_out)] , 'YData' , [2*sin(tm_in) 6*sin(tm_out)])
%% === calculate the next position of pointer of second =======
ts_out = -(ti(6)/60)*2*pi + pi/2 ;
ts_in = ts_out - pi;
set(hs, 'XData' , [3*cos(ts_in) 7*cos(ts_out)] , 'YData' , [3*sin(ts_in) 7*sin(ts_out)])
% light('position' , [-10,15,0] , 'style' , 'local' , 'color', [0.8,0.3,0.3]) drawnow ;
% pause(0.09) ;
end
catch
'error:please mail to 1917066897@https://www.doczj.com/doc/5511832154.html, '
return
end
end
%% === 子程序一闹钟程序======
%% === timing.m (注意使用原始文件名) ======
function [ ] = timing( obj, event )
%% === save the current time ========
ti = clock ;
%% === create a input dialog =========
prompt = {'mouth' , 'day' , 'hour' , 'minute' , 'event'} ;
title = 'input your timing clock time' ;
answer = inputdlg(prompt, title) ;
% disp(answer); %=== test code ========= %% === get the time form client =======
time_set = clock ;
time_set(2) = str2num( answer{1} ) ;
time_set(3) = str2num( answer{2} ) ;
time_set(4) = str2num( answer{3} ) ;
time_set(5) = str2num( answer{4} ) ;
time_set(6) = 0 ;
T_event{1,1} = answer{5} ;
% disp(time_set) ; %=== test code ========
%% === show your set time =========
str = ['你在' num2str(time_set(1)) '/' answer{1} '/' answer{2} ' ' answer{3} ':' answer{4} '设置了一个闹钟,内容为:' answer{5}] ;
% disp(str) ; % === test code =======
msgbox(str, 'attention') ;
%% === create a timer =============
h_timing = timer('Name' , 'H_timing' ,...
'TimerFcn' , {@T_ring , T_event},... %=调用了T_ring.m,并传参
'ExecutionMode' , 'singleShot' );
%% === start the timer =============
startat(h_timing, time_set) ;
end
%% === 子程序一铃声程序======
%% === T_ring.m (注意使用原始文件名) ====== function [ ] = T_ring( obj, event , T_event )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%% === save the content of cell - T_event =====
msg = T_event{1,1} ;
% disp(msg) ; % === test code ========
msgbox(msg, '你有一个闹铃提醒') ;
%% === 这是matlab 自带的铃声,其他的铃声程序可以在网上找=== for i = 1:5
load splat ;
sound(y, Fs) ;
end
end
%% === 子文件计时程序(注:这不是一个函数文件)=== %% === calc_time.m (注意使用原始文件名) ====== %% === create a figure ==========
h1 = figure( 'MenuBar' , 'none' ,...
'Color' , 'white' ,...
'NumberTitle' , 'off' ,...
'units','normalized' , 'position', [0.35 0.4 0.3 0.2], ...
'Name' , 'Calc_time' ) ;
%% ===
h1_text = uicontrol('parent', h1, ...
'BackgroundColor', [1 0.8 0.4], ...
'fontname' , 'Verdana' ,...
'fontsize' , 20 ,...
'style', 'text', ...
'horizontal', 'center', ...
'units','normalized' , 'position', [0.15 0.5 0.7 0.3], ...
'string', '0 : 0 : 0.0' );
%% ===
h1_button_start = uicontrol('parent', h1, ...
'BackgroundColor', [1 0.8 0.4], ...
'fontname' , 'Verdana' ,...
'fontsize' , 12 ,...
'style', 'pushbutton', ...
'horizontal', 'center', ...
'units','normalized' , 'position', [0.15 0.2 0.2 0.2], ...
'callback' , [' k=0 ;'] ,...
'string', 'start' );
%% ===
h1_button_stop = uicontrol('parent', h1, ...
'BackgroundColor', [1 0.8 0.4], ...
'fontname' , 'Verdana' ,...
'fontsize' , 12 ,...
'horizontal', 'center', ...
'units','normalized' , 'position', [0.40 0.2 0.2 0.2], ...
'callback' , [...
' k=1 ; , '] ,...
'string', 'stop' ) ;
%% ===
h1_button_reset = uicontrol('parent', h1, ...
'BackgroundColor', [1 0.8 0.4], ...
'fontname' , 'Verdana' ,...
'fontsize' , 12 ,...
'horizontal', 'center', ...
'units','normalized' , 'position', [0.65 0.2 0.2 0.2], ...
'callback' , [...
' k=2 ; , '] ,...
'string', 'reset' ) ;
%% ===
k = 2 ;
while ishandle(h1)
if ( 0 == k )
t_that = clock ;
while ishandle(h1)
t_this = clock ;
% ==
if ( t_this(6) >= t_that(6) )
t_second = t_this(6) - t_that(6) ;
else
if ( t_this(5) > t_that(5) )
t_this(5) = t_this(5) - 1 ;
t_second = t_this(6) + 60 - t_that(6) ;
else
t_this(4) = t_this(4) - 1 ;
t_this(5) = t_this(5) + 59 ;
t_second = t_this(6) + 60 - t_that(6) ;
end
end
% ==
if ( t_this(5) >= t_that(5) )
t_minute = t_this(5) - t_that(5) ;
else
t_this(4) = t_this(4) - 1 ;
t_minute = t_this(5) + 60 - t_that(5) ;
end
% ==
t_hour = t_this(4) - t_that(4) ;
% ==
set( h1_text , 'String' , [num2str(t_hour) ' : ' num2str(t_minute) ' : ' num2str(t_second)] ) ;
pause( 0.1 ) ; % === ! important pause in inside while =======
% ==
if ( k == 1 )
break ;
end
if ( k == 2 )
set( h1_text , 'String' , '0 : 0 : 0.0' ) ;
break ;
end
end %=== close inside while ==========
end
if ( k == 2 )
set( h1_text , 'String' , '0 : 0 : 0.0' ) ;
end
pause( 0.1 ) ; % === ! important pause in outside while ===========
end %=== close outside while ==========
摘要
使用matlan的画图函数,如:plot 、fill 等,设计了一个简约的圆形时钟GUI,并使用图形句柄实现了动态显示时间,自带图像背景,具有闹钟功能。
关键词:matlab时钟matlab闹钟clock timer matlab GUI背景matlab计时
1. 工具与函数
MATLAB
MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室)。是由美国mathworks公司发布的主要面对科学计算、可视化以及交互式程序设计的高科技计算环境。它将数值分析、矩阵计算、科学数据可视化以及非线性动态系统的建模和仿真等诸多强大功能集成在一个易于使用的视窗环境中,为科学研究、工程设计以及必须进行有效数值计算的众多科学领域提供了一种全面的解决方案,并在很大程度上摆脱了传统非交互式程序设计语言(如C、Fortran)的编辑模式,代表了当今国际科学计算软件的先进水平。
MATLAB和Mathematica、Maple并称为三大数学软件。它在数学类科技应用软件中在数值计算方面首屈一指。MATLAB可以进行矩阵运算、绘制函数和数据、实现算法、创建用户界面、连接其他编程语言的程序等,主要应用于工程计算、控制设计、信号处理与通讯、图像处理、信号检测、金融建模设计与分析等领域。
clock
clock 在matlab中用于获取系统时间,返回一个数组,保存系统时间,保存格式为[年月日时分秒] 。
>>clock
ans =
1.0e+03 *
2.0150 0.0060 0.0140 0.0170 0.0490 0.0175
plot
绘图函数plot,根据不同的坐标参数可以二维平面上绘制出不同的曲线。
语法:plot( X, Y, LineSpec )
X, Y为同维向量时,绘制以X,Y元素为横,纵坐标的一条曲线,LineSpec,用于指出线条的类型、标记符号和颜色。
figure
figure创建一个图形窗口,用法如下:
figure ; 创建一个图形窗口,所有属性皆为默认。
figure( '属性' , '属性值' ) ; 创建一个用户自定义的图形窗口。
h = figure( '属性' , '属性值' ) ; 创建一个图形窗口,并返回窗口的句柄。
uicontrol
Uicontrol是user interface control 的缩写(用户界面控制)。MATLAB控制框,又称uicontrol,与窗口管理器所用的函数十分相似。它们是图形对象,可以放置在MATLAB 的图形窗中的任何位置并用鼠标激活。MATLAB的uicontrol包括按钮、滑标、文本框及弹出式菜单。
语法:H=uicontrol((Hf_fig,' 属性' ,属性值,...)
其中,H是由函数uicontrol生成uicontrol对象的句柄,Hf_fig是父对象的句柄,它必须是图形,属性' Style ' 决定了所建控制框的类型。' Callback ' 属性值是当控制框激活时,传给eval在命令窗口空间执行的MATLAB字符串。
timer
鉴于Matlab 中缺乏多线程机制,使用Timer 无疑是一个很重要的工具,Matlab中Timer(定时器)是一个Java对象。
语法:t=timer( '属性' , 属性值) ;
属性:TimerFcn:Timer执行的函数,属性值‘@function_name’ ,就是你定义的函数名。
ExecutionMode:执行模式,决定定时器对象如何安排定时事件。属性值:'singleShot'(默认值) 、'fixedDelay'、'fixedRate'、'fixedSpacing' 。
Period:定时器触发周期(默认值为1s,数据类型double,最小定时时长0.001)。
StartDelay:指定从定时器开始到第一次执行callback函数的延时时长(数据类型double,值的范围:大于0的数,默认值为0)[即若加此属性,第一次定时时间为Period + StartDelay]。
StartFcn:定时器开启时的回调函数。
StopFcn:定时器停止时的回调函数
startat ( OBJ, [Y, M, D, H, MI, S] ) :在[Y, M, D, H, MI, S] 时间上启动timer,Y:年,M:月,D:日,H:时,MI:分,S:秒。
2. 设计思路
2.1.创建时钟的外层
我做的是一个典型的时钟,采用的圆形的外表。
实现:圆形外表:可以使用画图函数,如plot,ezplot,fplot等函数画圆。颜色填充:然后直接线条属性定义,加大其线条宽度,以及设置相应的颜色。
2.2.时钟的时针,分针,秒针
指针的外形:指针的外形,采用的是条形(矩形)的指针。
时针角度:使用clock获得当前时间,计算当前的时间的小时t,除以12,获得所转过角度的百分比,再乘以2*pi,得到时针转过的角度,但是,时针转过的角度,是从时钟“12点”位置顺时针开始算的,和我们使用x轴(即时钟“3点”位置)逆时针的计算方法有差别,所以需要矫正,对求出来的时针转过的角度去相反数,在加上pi/2,就可以实现从时钟“12”位顺时计算,转换到时钟“3点”逆时计算。
时针角度:同理,使用clock获得当前时间,计算当前的时间的分钟t,除以60,获得所转过角度的百分比,再乘以2*pi,得到分针转过的角度,但是,分针转过的角度,是从分针“12点”位置顺时针开始算的,和我们使用x轴(即时钟“3点”位置)逆时针的计算方法有差别,所以需要矫正,对求出来的分针转过的角度去相反数,在加上pi/2,就可以实现从时钟“12”位顺时计算,转换到时钟“3点”逆时计算。
时针角度:使用clock获得当前时间,计算当前的时间的秒数t,除以60,获得所转过角度的百分比,再乘以2*pi,得到秒针转过的角度,但是,秒针转过的角度,是从时钟“12点”位置顺时针开始算的,和我们使用x轴(即时钟“3点”位置)逆时针的计算方法有差别,所以需要矫正,对求出来的秒针转过的角度去相反数,在加上pi/2,就可以实现从时钟“12”位顺时计算,转换到时钟“3点”逆时计算。
初始位置:
方法一:使用fill函数,求出条形(矩阵)指针的四个角的坐标,然后用fill函数对四个角的坐标所围成的区域进行填充。保存fill的句柄。
方法二:使用plot函数,求出条形指针头(指向数字的一头)和尾的中点坐标,假设头坐标为(x1, y1),尾坐标为(x2, y2),可以这样写:
plot( [x2 x1], [y2 y1], 'LineWidth' , c1, 'color', [v1 v2 v3] ) ;
[x2 x1], [y2 y1]是plot函数需要描绘的坐标,这将会描绘出一条直线,使用'LineWidth'属性,增加直线的宽度,就能形成条形指针,再使用'color'属性(采用的256位颜色表,见附录),进行自己喜欢的颜色的填充。保存plot的句柄。
下一个位置:
方法一:更新时间,计算好位置,使用保存好的fill的句柄,设置'XData','YData' 属
性,把其改为更新的位置。
方法二:更新时间,计算好位置,使用保存好的plot的句柄,设置'XData','YData' 属性,把其改为更新的位置。
2.3.数字时钟
为了让用户更加清晰地知道时间,在物理时钟的下面,增设了一个数字时钟。
实现:创建一个edit控件,保存其句柄,获得当前时间,保存在一个数组里,用edit 的句柄设置String为当前时间的时分秒。
2.4.闹钟功能
实现:获得用户需要设置的时间,创建一个菜单,菜单的作用为调用闹钟函数。
闹钟功能闹钟函数的实现:创建一个输入对话框inputdlg,获取用户要设定的时间,以及需要提醒的事件,计算出与当前时间的时间差,创建一个计时器timer,作用为调用提醒函数,然后以时间差为延迟,启动timer。提醒函数的作用是调用铃声,以及显示用户所设定的事件。
2.5.背景
为了最大程度满足用户审美需求,使用imread , image函数设置相关的背景图,使时钟更生动。