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
執行結果: