您的位置:首页 > 编程语言 > MATLAB

matlab绘制实用日历实例代码

2015-02-16 20:02 337 查看
function TheStudy;%函数名
close all;%关闭所有床头
DD={'Sun','Mon','Tue','Wed','Thu','Fri','Sat'};%日历表头文字
figure;%打开一个窗口
%绘制窗口中的图标
uicontrol(gcf,'style','text','unit','normalized','position',[0.12,0.9,0.4,0.08],...
'ForegroundColor',[0.4,0.9,0.8],'BackgroundColor',[0.3,0.4,0.4],'fontsize',21,'String','The Calendar');
td=uicontrol(gcf,'style','push','unit','normalized',...
'position',[0.22,0.1,0.5,0.1],'fontname','default',...
'fontsize',18,'ForegroundColor',0.6*[1,1,1],'BackgroundColor','r');
%绘制日历的表头
for k=1:7
uicontrol(gcf,'style','text','unit','normalized',...
'position',[0.02+k*0.1,0.7,0.08,0.06],'fontsize',18,...
'ForegroundColor','r','BackgroundColor','b','String',DD{k});
end
%取出现在时间的字符串
timestr=datestr(now);
%时间付给年 月 日
[y,m,d]=datevec(timestr);
%本月的总天数
last=eomday(y,m);
%---------
%以下四行为求月初是星期几的算法。
%总结出来的规律
%假设目前为星期 x(1<=x<=7),日期号为d.
%得y=x+1+7-rem(d,7);
%∴本月初为星期z=rem(y,7)+1;
number_week=weekday(timestr);
remvalue=rem(d,7);
number_week=number_week+7-remvalue;
number_week=rem(number_week,7)+1;

row=0;
total_num=0;%记录画上图标的天数,不超过本月总天数(last)
bc='b';
while total_num<last
total_num=total_num+1;
if total_num==d
bc='r';
else
bc='w';
end
uicontrol(gcf,'style','push','ForegroundColor',bc,'BackgroundColor',0.4*[1,1,1],...
'String',num2str(total_num),'fontsize',18,'fontname','default','unit','normalized',...
'position',[0.02+number_week*0.1,0.6-row*0.08,0.08,0.06]);
if number_week==7
row=row+1;
number_week=1;
else
number_week=number_week+1;
end
end
try%循环显示当前时间
while 1
[y,m,d,h,mi,s]=datevec(datestr(now));
set(td,'String',[datestr(now,2),' ',datestr(now,13)]);
pause(1);
end
end


  执行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐