资源描述
用 MATLAB设计一个万年历
精品资料
课程论文
用 MATLAB设计一个万年历
课 程
数学软件
学生姓名
学 号
手机号码
所在学院
理学院
所在班级
信计1134
任课教师
提交时间
2015年 06 月 07日
目录
万年历的效果图………………………………………………………………………………………………………………………2
第一章 设计目的与要求...........................................................................................................3
1.1 设计目的……………………………….………………………………………………………………………………………….3
1.2 设计要求…………………………………………………………………………………………………………………………..3
第二章 GUI界面设计……………………………………………………………………………………………………………3
2.1 打开GUI …………………………………………………………………………………………………………………………..3
2.2 添加按钮控件…………………………………………………………………………………………………………………...4
2.3 根据控件的属性及视觉效果做一定的修改……………………………………………………………………..5
2.4 保存、添加功能函数………………………………………………………………………………………………………..5
2.4.1 在“nian_edit1_Callback”中编写代码………………………………………………………………….5
2.4.2 在“yue_edit2_Callback”中编写代码…………………………………………………………………..6
2.4.3 在“zhou_pushbutton1_Callback”中编写代码…………………………………………………….6
2.4.4 在“rili_pushbutton2_Callback”中编写代码………………………………………………………..6
第三章 万年历的使用…………………………………………………………………………………………………………..7
3.1 运行…………………………………………………………………………………………………………………………………..8
3.2 输入…………………………………………………………………………………………………………………………………..8
3.3 错误警告…………………………………………………………………………………………………………………………..9
第四章 改写界面………………….………………………………………………………………………………………………10
第五章 结论、问题解决及改进...............................................................................................10
5.1结论与讨论………………………………………………………………………………………………………………………..10
5.2问题解决……………………………………………………………………………………………………………………………10
5.3可以加以改进的地方…………………………………………………………………………………………………………10
第六章 心得体会…………………………………………………………………………………………………………………..11
参考文献…………………………………………………………………………………………………………………………………..11
附录:源程序代码……………………………………………………………………………………………………………………11
万年历效果图
第一章 设计目的与要求:
1.1设计目的:
通过万年历程序的设计,培养学生综合利用MATLAB进行程序设计的能力,加强函数的运用,提高软件系统分析能力和程序文档建立、归纳总结的能力,培养学生利用系统提供的标准函数及典型算法进行设计,开发应用软件。
通过本项课程设计,可以培养独立思考、 综合运用所学有关相应知识的能力,能更好的巩固《数学软件》课程学习的内容,掌握 GUI界面设计的基本方法,更加了解了MATLAB的好处和其可用性!
1.2设计要求:
设计一个万年历GUI界面,其界面布局如上图,包括“输入年份”、“输入月份”、“显示星期”、
“显示日历”等控件。在界面上任意输入某个具体年份和月份,单击按钮即可显示本月的日历及其对应的星期(0表示没有数字日期)。
第二章 GUI界面设计:
2.1打开GUI
输入Guide回车或者在工具栏上点击图标打开Guide窗口:
2.2添加按钮控件
1、选取5个静态文本控件,用来定义“输入年份”、“输入月份”、“星期”、“当月日历显示区”,以及显示每周的“日”、“一”、“二”、“三”、“四”、“五”、“六”;
2、选取两个push button按钮空间用来定义“显示星期”、“显示日历”;
3、添加两个编辑文本框控件用来输入具体的数字年份和月份;
4、添加42个编辑文本框控件,用来显示具体的日期。(具体摆放如下图)
2.3 根据控件的属性及视觉效果做一定的修改
1、双击“输入年份”、“输入月份”、“星期”和“当月日历显示区”这4个静态文本框,在“String”文本框中输入相应的中文,将字体大小“FontSize”设置为20,其他默认即可。
2、对于“星期”下方的静态文本框,“String”文本框中不输入任何内容,将字体大小“FontSize”设置为25,在“Tag”文本框中输入“xingqi_text4”。
3、对于输入年份和月份的编辑文本框,在“String”文本框中输入0,将字体大小“FontSize”设置为18,在“Tag”文本框中分别输入“nian_edit1”和“yue_edit2”。
4、对于两个push button按钮,在“String”文本框中分别输入“显示星期”和“显示日期”,将字体大小“FontSize”都设置为20,在“Tag”文本框中分别输入“zhou_pushbutton1”和“rili_pushbutton2”。
5、对于42个编辑文本框,首先将“Enable”属性中的“on”设置为“inactive”,使其转为静态文本框,并且保持控件的高亮状态;其次,在“String”文本框中都不输入任何内容;最后在“Tag”文本框中从左到右、按列依次输入“r1_edit”、“r2_edit”、···、“r42_edit”。
2.4保存、添加功能函数
把做好的按钮及静态文本框保存为“wannianli.fig”后自动弹出Editor的M文本,然后对相应的控件添加功能函数。以下是相应控件的功能函数的代码。(单击工具栏中的按钮可快速跳转到各个控件的回调函数)
2.4.1在“nian_edit1_Callback”中编写代码
function nian_edit1_Callback(hObject, eventdata, handles)
% 添加如下程序:
input=str2num(get(hObject,'String')); % 输入年份
if(isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
2.4.2在“yue_edit2_Callback”中编写代码
function yue_edit2_Callback(hObject, eventdata, handles)
input=str2num(get(hObject,'String')); % 输入月份
if(isempty(input))
set(hObject,'String','0')
end
if input>=13
errordlg('月份不能超过12','警告') % 显示警告信息库
end
guidata(hObject,handles);
2.4.3在“zhou_pushbutton1_Callback”中编写代码
function zhou_pushbutton1_Callback(hObject, eventdata, handles)
% 添加如下程序:
h={'日';'一';'二';'三';'四';'五';'六'}; % 显示结果
set(handles.xingqi_text4,'String',h); % 更新结构体
guidata(hObject,handles);
2.4.4在“rili_pushbutton2_Callback”中编写代码
function rili_pushbutton2_Callback(hObject, eventdata, handles)
% 添加如下程序:
nian=get(handles.nian_edit1,'String');
yue=get(handles.yue_edit2,'String');
year=str2num(nian);
month=str2num(yue);
%找出各年12个月的天数
for m=1:12
if mod(year,4)==0&mod(year,100)~=0|mod(year,400)==0
D=[31 29 31 30 31 30 31 31 30 31 30 31];
else
D=[31 28 31 30 31 30 31 31 30 31 30 31];
end
Y=D(1:m);
end
%定义初始值
run=0; %闰年初始值
ping=0; %平年初始值
%计算从第一年到前一年的闰年和平年的个数
for q=1:year-1
if(mod(q,4)==0&mod(q,100)~=0)|mod(q,400)==0
run=run+1;
else
ping=ping+1;
end
end
%计算从第一年到当年前一个月的天数
S=366*run+365*ping;
for p=1:month-1
S=S+Y(p);
end
%获得这个月的天数
n=Y(month);
A=zeros(n,1);
S=S+1;
%计算这个月的第一天是星期几
w=mod(S,7);
for k=1:n
A(w+k)=k;
end
T=[A(1:end);zeros(42-length(A),1)]; %没有日期用0代替
set(handles.r1_edit,'String',num2str(T(1))); %显示结果
set(handles.r2_edit,'String',num2str(T(2)));
set(handles.r3_edit,'String',num2str(T(3)));
%以下类推,直到r40_edit(在此省略,但在M文件上必须全部写上)
%_________________________________________________
set(handles.r41_edit,'String',num2str(T(4)));
set(handles.r42_edit,'String',num2str(T(5)));
guidata(hObject,handles);
第三章 万年历的使用
3.1运行
单击本M文件编辑窗口中工具栏中的“运行”按钮,或单击GUIDE的输出编辑器中的工具栏中的按钮,创建功能GUI界面,如下图
3.2输入
在图中输入年份为“2015”,输入月份为“2”,单击“显示星期”按钮和“显示日历”按钮,显示结果如下图
3.3错误警告
若在“输入月份”文本框中输入的数字超过12,则会弹出警告对话框提示错误。例如输入月份“13”,则弹出如下图所示警告窗口。
第四章 改写界面
注意到上图左上角名称为“万年历”而不是“wannianli”,其实修改很简单,只需在输出编辑器界面中,单击鼠标右键,从弹出的快捷菜单中选择“Property Inspector”命令,打开界面属性窗口。将窗口的标题(Name属性)设置为“万年历”,关闭该窗口,并运行界面,显示结果就是创建的“万年历”窗口。
在图中输入年份为“2015”,输入月份为“6”,单击“显示星期”按钮和“显示日历”按钮,显示结果就是所要创建的GUI界面。
第五章 结论、问题解决与改进
5.1结论与讨论:
经过多次实验,本程序进行本月日历的显示及对应的星期的结果符合事实。
5.2问题解决:
如出现界面数字混乱,可能是因为42个文本框没有从上到下依次添加,可以交换编辑文本框位置,直到显示的日历符合事实为止。
5.3可以加以改进的地方:
1.可对GUI界面进行适当美化
2.可增加农历
3.加如节日和农历节气的显示
第六章 心得体会
目前流行的计算机日历程序,比较典型的是Windows各版本中的日历程序以及基础于该程序所开发的各种应用程序中的日历程序。然而,这些程序都千篇一律的局限在一个很短的时间范围内。(Windows各个版本一般都局限在1980年至2099年这一范围内),但是,在很多情况下,特别是在众多的科学研究领域中,一个时间跨度较大的日历程序是很有参考价值的。
经过一个学期对数学软件的学习,我们学习了理论知识,了解了MATLAB这个软件,这些知识都为我们的下一步学习打下了坚实的基础。通过课程设计,一方面是为了检查我们一个学期来我们学习的成果,另一方面也是为了让我们进一步的掌握和运用它,同时也让我们认清自己的不足之处和薄弱环节,加以弥补和加强。
通过本次的MATLAB设计,让我对MATLAB尤其是GUI可视化图形界面的设计功能有了进一步的了解,认识到其功能的强大和丰富的内置函数及工具箱。在MATLAB万年历的设计中,了解关于MATLAB图形用户界面的部分空间的使用方法,利用MATLAB的GUI提供的很多实用控件,方便用于设计自己的图形界面。
在万年历的编写过程中也体会到了做事情一顶要细心、认真。更加知道了要掌握好基础知识。还有体会到了成功的感觉!通过本项课程设计也培养了我独立思考、 综合运用所学有关相应知识的能力。
参考文献
【1】杨德平、赵维加、管殿柱.MATLAB基础教程.机械工业出版社.2014.11
【2】施晓红、周佳.精通GUI图形界面教程[M].北京:北京理工大学出版社.2003.
【3】罗华飞.MATLAB GUI设计学习手记[M].北京:北京航空航天大学出版社.2009.8.1
附录:程序源代码
function varargout = wannianli(varargin)
% WANNIANLI MATLAB code for wannianli.fig
% WANNIANLI, by itself, creates a new WANNIANLI or raises the existing
% singleton*.
%
% H = WANNIANLI returns the handle to a new WANNIANLI or the handle to
% the existing singleton*.
%
% WANNIANLI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WANNIANLI.M with the given input arguments.
%
% WANNIANLI('Property','Value',...) creates a new WANNIANLI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before wannianli_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to wannianli_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help wannianli
% Last Modified by GUIDE v2.5 17-May-2015 21:12:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @wannianli_OpeningFcn, ...
'gui_OutputFcn', @wannianli_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before wannianli is made visible.
function wannianli_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to wannianli (see VARARGIN)
% Choose default command line output for wannianli
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes wannianli wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = wannianli_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function nian_edit1_Callback(hObject, eventdata, handles)
% hObject handle to nian_edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of nian_edit1 as text
% str2double(get(hObject,'String')) returns contents of nian_edit1 as a double
input=str2num(get(hObject,'String'));
if(isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function nian_edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to nian_edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in zhou_pushbutton1.
function zhou_pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to zhou_pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h={'日';'一';'二';'三';'四';'五';'六'};
set(handles.xingqi_text4,'String',h);
guidata(hObject,handles);
function r1_edit_Callback(hObject, eventdata, handles)
% hObject handle to r1_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of r1_edit as text
% str2double(get(hObject,'String')) returns contents of r1_edit as a double
% --- Executes during object creation, after setting all properties.
function r1_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to r1_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function r2_edit_Callback(hObject, eventdata, handles)
% hObject handle to r2_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of r2_edit as text
% str2double(get(hObject,'String')) returns contents of r2_edit as a double
% --- Executes during object creation, after setting all properties.
function r2_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to r2_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function r3_edit_Callback(hObject, eventdata, handles)
% hObject handle to r3_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of r3_edit as text
% str2double(get(hObject,'String')) returns contents of r3_edit as a double
% --- Executes during object creation, after setting all properties.
function r3_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to r3_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function r4_edit_Callback(hObject, eventdata, handles)
% hObject handle to r4_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of r4_edit as text
% str2double(get(hObject,'String')) returns contents of r4_edit as a double
% --- Executes during object creation, after setting all properties.
function r4_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to r4_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function r5_edit_Callback(hObject, eventdata, handles)
% hObject handle to r5_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Str
展开阅读全文