📄 mmclock.m
字号:
function T=mmclock(X,Y)
%MMCLOCK Place a digital clock on the screen.
% MMCLOCK places a digital clock at the upper-right corner
% of the display screen.
% MMCLOCK(X,Y) places a digital clock at position X pixels
% to the right and Y pixels above the bottom of the screen.
% T=MMCLOCK returns the current date and time as a string
% when ' Close ' is pressed.
%设定初始值,分析输入参量。
fsize=[200 150]; sec=1; mil=0;
mstr=[ ' Jan ' ; ' Feb ' ; ' Mar ' ; ' Apr ' ; ' May ' ; ' Jun '
' Jul ' ; ' Aug ' ; ' Sep ' ; ' Oct ' ; ' Nov ' ; ' Dec ' ];
scr=get(0, 'ScreenSize' );
if nargin==0
figpos=[scr(3)-fsize(1)-200 scr(4)-fsize(2)-50 fsize(1:2)];
elseif nargin==2
figpos=[X Y fsize(1:2)];
else
error( ' Invalid Arguments ' );
end
%建立图形,在图中设置uicontrol为某些缺省值。
Hf_clock=figure( 'Position' ,figpos ,...
'Color' ,[.7 .7 .7],...
'NumberTitle' , 'off ' ,...
'Name' , ' Digital Clock ' );
set(Hf_clock, 'DefaultUicontrolUnits' , 'normalized' ,...
'DefaultUicontrolBackgroundColor' ,get(Hf_clock, 'Color' ));
%对秒建立Close 按钮键、无线按钮;对24小时建立检查框。
Hc_close=uicontrol( 'Style' , 'push' ,...
'Position' ,[.65 .05 .30 .30],...
'BackgroundColor' ,[.8 .8 .9],...
'String' , 'Close' ,...
'CallBack' , 'close(gcf)' );
Hc_sec=uicontrol( 'Style' , 'radiobutton' ,...
'Position' ,[.05 .05 .50 .13],...
'Value' ,sec,...
'String' , 'Seconds' );
Hc_mil=uicontrol( 'Style' ,'checkbox' ,...
'Position' ,[.05 .22 .50 .13],...
'Value' ,mil,...
'String' , '24-Hour' );
%对日期和时间显示创建框架和文本串。
Hc_dframe=uicontrol( 'Style' , 'frame' , 'Position' ,[.04 .71 .92 .24]);
Hc_date =uicontrol( 'Style' , 'text' , 'Position' ,[.05 .72 .90 .22]);
Hc_tframe=uicontrol( 'Style' , 'frame' , 'Position' ,[.04 .41 .92 .24]);
Hc_time=uicontrol( 'Style' , 'text' , 'Position' ,[.05 .42 .90 .22]);
%循环,直到按钮键回调函数关闭图形,每秒更新显示一次。
while find(get(0, 'Children' )) == Hf_clock % Looop while clock exists
sec = get(Hc_sec, 'Value' );
mil = get(Hc_mil, 'Value' );
now1=fix(clock);
datestr=sprintf( '%s %2d %4d' ,mstr(now1(2),now1(3)),now1(1));
if mil
suffx= ' ' ;
else
if now1(4)>12
suffix= ' pm ' ;
now1(4)=rem(now1(4),12);
else
suffix= ' am ' ;
end
end
timestr=[num2str(now1(4)) : sprintf( '%02d ' ,now1(5))];
if sec
timestr=[timestr : sprintf( '%02d,now1(6)')];
end
timestr=[timestr suffix];
set(Hc_date, 'String' ,datestr);
set(Hc_time, 'String' ,timestr);
pause(1)
end
%最后,如果有需,返回日期和时间字符串。
if nargout
T=[datestr ' - ' timestr];
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -