11/28/2006

小程式 月曆產生器


把程式加入介面


GUIDE程式說明

首先在command window下打guide就會出現下圖


接下來選Blank GUI(Default),就會開啟新的window,如下圖


接下來你會看到左邊有很多控制物件,
你只要以滑鼠點選其中一項
並把它拖到你想要的位置就可以.

月曆這個程式我有用到[push button]二個(run run)
九個[text](如萬年曆、月、年和藍底的框都是)
而其它的空白框則都是[edit](可以key in數字的)
如下圖
(小格子是中文字,因為我7版的不能用,所以我就用6.5版的
,在此要注意,應該會有些指令在6.5不能用)



而要是想更改其顏色,字體等則在其物件點擊二下則會出現下圖(這是以紅色的run為例子)
或是mark其物件,選View,再選Property Inspector也可以


當你選擇好你要用的物件,即可存檔。此時你會存成二個檔
一個叫 xxx.fig的檔,另一個是xxx.m的檔。接下來則是程式編輯的部份了,
點選View,再選M-file Editor即可開始編輯程式。
當你一開啟M-file你就會看到密密麻麻的字,但在這個程式會用到的只有幾個而已。

大部份我們都是在function 物件名稱_callback(hObject, eventdata, handles)
下編輯其程式,如下圖:

這個程式的[edit物件名稱]空白處的程式碼為
function edit物件名稱_Callback(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
newstrval=get(hObject,'string'); % 截取輸入的字串
newval=str2double(newstrval); % 變成數字
handles.edit物件名稱=newval; % 存儲起來
guidata(hObject,handles);
% Hints: get(hObject,'String') returns contents of edit8 as text
% str2double(get(hObject,'String')) returns contents of edit8 as a double
以下為pushbutton的圖

這個程式的[pushbutton物件名稱]的程式碼為

Function pushbutton物件名稱_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton3 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

Y=handles.edit6; % 截取年的值
M=handles.edit7; %
截取月的值
K=handles.edit8; % 截取日的值

% 以下為判斷你輸入的日子是星期幾

if rem(Y,100)==0 & rem(Y,400)==0 | rem(Y,4)==0
d1=[31 29 31 30 31 30 31 31 30 31 30 31];
else
d1=[31 28 31 30 31 30 31 31 30 31 30 31];
end
s=Y-1;
c=d1(1:(M-1));
c1=sum(c)+K;
ss=s+fix(s/4)-fix(s/100)+fix(s/400)+c1;
dd=rem(ss,7)+1;
di=['','','','','','',''];
ggo=sprintf('你查詢的%d%d%d日是星期%s',Y,M,K,di(dd));
set(handles.text12,'string',ggo); % 把它show出來在藍色框框中如下圖



另一個[pushbutton物件名稱]的程式碼為
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Y=handles.edit9; % 讀取年的值
M=handles.edit10; % 讀取用的值
ans=[ ];
if rem(Y,100)==0 & rem(Y,400)==0 | rem(Y,4)==0
d1=[31 29 31 30 31 30 31 31 30 31 30 31];
else
d1=[31 28 31 30 31 30 31 31 30 31 30 31];
end
s=Y-1;
c=d1(1:(M-1));

% 以下為建月曆的運算式
for k=1:d1(M)
c1=sum(c)+k;
ss=s+fix(s/4)-fix(s/100)+fix(s/400)+c1;
ans(k)=rem(ss,7);
end
ans(ans==0)=7;
pp=ans(1);
ans1=[ ];

k1=0;
for n=1:6;
for m1=pp:7
ans1(n,m1)=k1;
k1=k1+1;
end
pp=1;
end
ans1(ans1>d1(M))=0; % 結果
% 以下就是把所出來的值給代入小格子
(這個東東,還可以修改)

set(handles.e1,'string','');
set(handles.e2,'string','
');
set(handles.e3,'string','');
set(handles.e4,'string','
');
set(handles.e5,'string','
');
set(handles.e6,'string','');
set(handles.e7,'string','');

set(handles.e8,'string',num2str(ans1(1,1)));
set(handles.e9,'string',num2str(ans1(1,2)));
set(handles.e10,'string',num2str(ans1(1,3)));
set(handles.e11,'string',num2str(ans1(1,4)));
set(handles.e12,'string',num2str(ans1(1,5)));
set(handles.e13,'string',num2str(ans1(1,6)));
set(handles.e14,'string',num2str(ans1(1,7)));

set(handles.e15,'string',num2str(ans1(2,1)));
set(handles.e16,'string',num2str(ans1(2,2)));
set(handles.e17,'string',num2str(ans1(2,3)));
set(handles.e18,'string',num2str(ans1(2,4)));
set(handles.e19,'string',num2str(ans1(2,5)));
set(handles.e20,'string',num2str(ans1(2,6)));
set(handles.e21,'string',num2str(ans1(2,7)));

set(handles.e22,'string',num2str(ans1(3,1)));
set(handles.e23,'string',num2str(ans1(3,2)));
set(handles.e24,'string',num2str(ans1(3,3)));
set(handles.e25,'string',num2str(ans1(3,4)));
set(handles.e26,'string',num2str(ans1(3,5)));
set(handles.e27,'string',num2str(ans1(3,6)));
set(handles.e28,'string',num2str(ans1(3,7)));

set(handles.e29,'string',num2str(ans1(4,1)));
set(handles.e30,'string',num2str(ans1(4,2)));
set(handles.e31,'string',num2str(ans1(4,3)));
set(handles.e32,'string',num2str(ans1(4,4)));
set(handles.e33,'string',num2str(ans1(4,5)));
set(handles.e34,'string',num2str(ans1(4,6)));
set(handles.e35,'string',num2str(ans1(4,7)));

set(handles.e36,'string',num2str(ans1(5,1)));
set(handles.e37,'string',num2str(ans1(5,2)));
set(handles.e38,'string',num2str(ans1(5,3)));
set(handles.e39,'string',num2str(ans1(5,4)));
set(handles.e40,'string',num2str(ans1(5,5)));
set(handles.e41,'string',num2str(ans1(5,6)));
set(handles.e42,'string',num2str(ans1(5,7)));

set(handles.e43,'string',num2str(ans1(6,1)));
set(handles.e44,'string',num2str(ans1(6,2)));
set(handles.e45,'string',num2str(ans1(6,3)));
set(handles.e46,'string',num2str(ans1(6,4)));
set(handles.e47,'string',num2str(ans1(6,5)));
set(handles.e48,'string',num2str(ans1(6,6)));
set(handles.e49,'string',num2str(ans1(6,7)));

下面為顯示結果圖

等程式都key in完,就可以按run,即可執行程式
月曆產生器檔案下載

2 則留言:

Eric Wu 提到...

我覺得這個程式真是太酷了,為了讓這個程式更frendly一點,於是貢獻一點心力。
我把中間存檔的部份改成
[filename, pathname, filterindex] = uiputfile({'*.xls','Exel Files (*.xls)';'*.*', 'All Files (*.*)'},'Save as');
file=[pathname filename];
s=xlswrite(file,{[y],' 年',[m],' 月'});
s=xlswrite(file,{'星期日','星期一','星期二','星期三','星期四','星期五','星期六'},...
'a2:g2');
s=xlswrite(file,ans1,'a3:g8');
as=sprintf(['你的月曆已經建好了\n\n','請到你的儲存路徑下打開%s這個檔\n\n'],filename);
msgbox(as)

這樣可以用存檔介面決定檔名。
執行圖1
執行圖2
執行圖3

ncbee 提到...

這個GUIDE我不會很熟,只是有興趣去找資料來看,所以只是懂皮毛,要是有錯請多多指教