《数学软件》课程设计论文用matlab做一个小闹钟

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《数学软件》课程设计论文用matlab
做一个小闹钟
《数学软件》课程设计广东海洋大学教师评语:《数学软件》课程设计MATLAB 设计一个时钟和闹钟院(系)名称理学院专业班级信计1132 姓名杨剑鸿学号201311921229 指导教师李志成绩指导教师签字:2015年6月15日用《数学软件》课程设计摘要:MATLAB 是一款强大的、应用广泛的软件,主要利用MATLAB 的相关编程来设计实现时钟界面显示效果关键词:时间、闹钟、MATLAB、界面目的:创建一个时钟与闹钟小软件,用于看时间,还有修改时间并且设定闹钟。

还能重用历史用过的闹钟作为闹钟,但仅编了只能设置一个闹钟,可以添加程序达到设置多
个闹钟。

功能介绍:此小软件能调整时间,以钟表方式显示时间,设置闹钟能打开或者关闭闹钟,并且记录历史调节过的前5个闹钟,提供方便再使用曾经用过的闹钟。

界面布局:见下图创建过程:1.加入如图的各种控件;安排好位置,点击’Align Objects’按钮对控件对齐。

2.改变各控件相关的属性值:的‘string’都改为’0’,并将其将坐标文本框下面的三个可编辑文本1《数学软件》课程设计‘tag’属性分别改为’hour’、’min’、’sec’。

以便在添加代码时能更快找到对应函数。

坐标文本框旁边的三个可编辑文本的‘string’都改为’ ‘,并将其‘tag’属性分别改为’hour _naozhong’、’min _naozhong’、’sec _naozhong’。

以便在添加代码时能更快找到对应函数。

将这四个静态文本框的’String’改为’:’。

将坐标文本框右上角的静态文本框的‘string’改为’●’。

将坐标文本框左下角的静态文本
框文本框的‘string’改为’修正时间:’。

将历史闹钟显示的2个静态的‘string’分别改为’历史闹钟:’、’时-分-秒’。

将数字的‘string’分别改为’1.’、’2.’、’3.’、’4.’、’5.’。

的‘string’都改为’ ‘。

并将其‘tag’标号的5个静态文本框将历史闹钟的5个静态文本框属性分别改为’naozhong1’、’naozhong2’、’naozhon g3’、’naozhong4’、’naozhong5’,以便在添加代码时能更快找到对应函数。

将坐标文本框右上角的静态文本框关闭闹钟。

将坐标文本框下面3个按钮的‘string’都改为’▲’,并将其‘tag’的‘string’改为’ ‘。

用以提示是否开启或者属性分别改为’hour_up’、’min_up’、’sec_up’,以便在添加代码时能更快找到对应函数。

将坐标文本框下面3个按钮的‘string’都改为’▼’,并将其‘tag’属性分别改为’hour_down’、’min_down’、’sec_down’,以便在添加代码时能更快找到对应函
数。

将按钮的‘string’都改为’确认修改’,并将其‘tag’属性分别改为’queren_time’,以便在添加代码时能更快找到对应函数。

将坐标文本框右边2个按钮的‘string’分别改为’打开’、’关闭’,并将其‘tag’属性分别改为’2《数学软件》课程设计open_naozhong’、’off_naozhong’,以便在添加代码时能更快找到对应函数。

将历史闹钟下面的5个按钮的‘string’都改为’设此为新闹钟’,并将其‘tag’属性分别改为’xinnaozhong1’、’xinnaozhong2’、’xinnaozhong3’、’xinnaozhong4’、’xinnaozhong5’,以便在添加代码时能更快找到对应函数。

将静态文本框的’backgroundcolor’改为灰色。

完成以上过程后,保存GUI为’时钟与闹钟.fig’。

3. 编写回调函数代码:黄色显示部分为自己添加的代码,紫色为函数的名称。

以下是主调函数的编辑:function
naozhong_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 naozhong (see V ARARGIN) % Choose default command line output for naozhong = hObject; =cell(1,5);%记录历史闹钟的排序% Update handles structure guidata(hObject, handles); 以下是3个调节时间增加的按钮的编码:小时:function hour_up_Callback(hObject, eventdata, handles) % hObject handle to hour_up (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a=str2num(get(,’string’)); %小时按键控制,若上调大于等于24,侧变回凌晨a=a+1; 3《数学软件》课程设计if a>=24a=0; end set(,’string’,a) guidata(hObject, handles); 分钟:function min_up_Callback(hObject, eventdata, handles) % hObject handle to min_up (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) M=str2num(get(,’string’));
H=str2num(get(,’string’));M=M+1; if M>=60%分钟按键控制,若上调大于等于60,侧变回0,并且小时进1 M=0;H=H+1; if H>=24%小时进1时,若大于等于24,则小时变回0H=0;end set(,’string’,H) end set(,’string’,M) guidata(hObject, handles); 秒钟:function sec_up_Callback(hObject,
eventdata, handles) % hObject handle to sec_up (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) S=str2num(get(,’string’));
M=str2num(get(,’string’));
H=str2num(get(,’string’)); S=S+1; if S>=60%秒的按键控制,若大于等于60,则分钟进1,秒变回0S=0; M=M+1; if M>=60%分钟进1时,若分钟大于等于60,则小时进1,分钟变回0M=0;H=H+1; if H>=24%小时进1时,若大于等于24,则小时变回0H=0;end set(,’string’,H)end set(,’string’,M) end 4
《数学软件》课程设计set(,’string’,S) guidata(hObject, handles); 以下是3个调节时间增加的按钮的编码:小时:function
handles) % hObject handle to hour_down (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) H=str2num(get(,’string’)); %小时按键控制,若上调小于0,侧变回23H=H-1; if H set(,’string’,H) gui data(hObject, handles); 分钟:function min_dowm_Callback(hObject, eventdata, handles) % hObject handle to min_dowm (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) M=str2num(get(,’string’));
H=str2num(get(,’string’)); M=M-1; if M if H set(,’string’,H) end set(,’string’,M) guidata(hObject, handles); 秒钟:% --- Executes on button press in sec_dowm. function
handles) % hObject handle to sec_dowm (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) S=str2num(get(,’string’));
M=str2num(get(,’string’));
H=str2num(get(,’string’)); S=S-1; 5《数学软件》课程设计if S if M if H>=-1%若小时减1时,小时小于0,则,小时变回23H=23;end set(,’string’,H)end set(,’string’,M) end set(,’string’,S) guidata(hObject, handles); 以下是确认修改按钮的编码:function queren_time_Callback(hObject, eventdata, handles) % hObject handle to queren_time (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see
GUIDATA) a3=str2num(get(,’string’)); a2=str2num(get(,’string’));
a1=str2num(get(,’string’));if a3>=25|a3=61|a2=61|a1errordlg(‘时间输入错误,请输入正确时间!’,’警告’) elseif
mod(a2,1)~=0|mod(a3,1)~=0|mod(a1,1)~= 0errordlg(‘请输入整数!’, ‘警告’) else
n3=str2num(get(,’string’));%控制时针n2=str2num(get(,’string’));%控制分针n1=str2num(get(,’string’));%控制秒针a=1;%a用来判定时间是否为0~12点或者12~24点,若为0~12则为1,否则为0if n3>12%若时刻为12~24,则要转换a=0;%a用来判定时间是否为0~12点或者12~24点,若为0~12则为1,否则为0 n3=n3-12;end n3=n3*5; a0=floor(n2/12); %用于分针每走12分钟,时针移动1/5小时n3=n3+a0;
M=moviein(61);%形成一个大小为61的动画矩阵6《数学软件》课程设计while n3switch a%判定输入的时刻是否为0~12,否则为12~24case 0 set(,’string’,floor(n3/5+12))case 1 set(,’string’,floor(n3/5)) end set(,’string’,n2) set(,’string’,n1)
nzH=get(_naozhong,’string’);%获得闹钟时刻nzM=get(_naozhong,’string’);%获得闹钟分钟nzS=get(_naozhong,’string’);%获得闹钟秒钟if nzH~=‘ ‘&nzM~=‘ ‘&nzS~=‘ ‘ %若都不为空则有闹钟设置switch a%判定输入的时刻是否为0~12,否则为12~24case 0if str2num(nzH)==floor(n3/5+12)&str2num( nzM)==n2&str2num(nzS)==n1
warndlg(‘闹钟时间已到!’,’闹钟’)
end case 1if str2num(nzH)==floor(n3/5)&str2num(nz M)==n2&str2num(nzS)==n1
warndlg(‘闹钟时间已到!’, ‘闹钟’) end end end syms t;x=sin(t);y=cos(t); ezplot(x,y);%钟的框架hold on axis equal for i=1:60%钟的框架y=*sin(pi/30*(15-i))+;
x=*cos(pi/30*(15-i))-;if mod(i,5)==0if i==60 text(x,y,’12’)else text(x,y,’I’)end else text(x,y,’.’) 7《数学软件》课程设计end hold on end quiver(0,0,*sin(n2*pi/30),*cos(n2*pi/30));
%分针的图形quiver(0,0,*sin(n3*pi/30),*cos(n3*pi/30));
%时针的图形quiver(0,0,*sin(n1*pi/30),*cos(n1*pi/30));
%秒针的图形
M(:,n1+1)=getframe; %将图形转换到M矩阵里面hold off pause() n1=n1+1;end if n1==60%秒针循环n1=0; n2=n2+1;end end if n2==60%分钟循环n2=0; n3=n3+1;end if n3==60%小时的循环n3=0;end end end guidata(hObject, handles); 以下是3个闹钟可编辑文本框的编码:function
hour_naozhong_Callback(hObject, eventdata, handles) % hObject handle to hour_naozhong (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 hour_naozhong as text % str2double(get(hObject,’String’)) returns contents of hour_naozhong as a double
in put=str2num(get(hObject,’string’));
%若没设置闹钟,则显示空if isempty(input) set(hObject,’string’,’ ‘) end guidata(hObject, handles); 8《数学软件》课程设计function min_naozhong_Callback(hObject, eventdata, handles) % hObject handle to min_naozhong (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 min_naozhong as text % str2double(get(hObject,’String’)) returns contents of min_naozhong as a double input=str2num(get(hObject,’string’));
%若没设置闹钟,则显示空if isempty(input) set(hObject,’string’,’ ‘) end guidata(hObject, handles); function sec_naozhong_Callback(hObject, eventdata, handles) % hObject handle
to sec_naozhong (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 sec_naozhong as text % str2double(get(hObject,’String’)) returns contents of sec_naozhong as a double input=str2num(get(hObject,’string’));
%若没设置闹钟,则显示空if isempty(input) set(hObject,’string’,’ ‘) end guidata(hObject, handles); 以下是2个闹钟打开与关闭按钮的编码:,则弹出文字显示打开闹钟!此处用到主调函数)function open_naozhong_Callback(hObject, eventdata, handles) % hObject handle to open_naozhong (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see
GUIDATA) % Hint: get(hObject,’Value’) returns toggle state of open_naozhong 9
《数学软件》课程设计H=str2num(get(_naozhong,’string’));
%获得闹钟时刻的字符串M=str2num(get(_naozhong,’string’));
%获得闹钟分钟的字符串S=str2num(get(_naozhong,’string’));
%获得闹钟秒钟的字符串d1=str2num(get(,’string’)); %用于判断历史闹钟的排序d2=str2num(get(,’string’)); %并记录在主调函数h细胞数组中d3=str2num(get(,’string’));
d4=str2num(get(,’string’));
d5=str2num(get(,’string’)); if isempty(d1)%判断历史闹钟1是否为空,若是则设置主调函数的h的第1个细胞,否则向下运作a1=1;a2=a1; elseif isempty(d2)%判断历史闹钟
2是否为空,若是则设置主调函数的h 的第2个细胞,否则向下运作a1=2;a2=a1; elseif isempty(d3)%判断历史闹钟3是否为空,若是则设置主调函数的h的第2个细胞,否则向下运作a1=3;a2=a1; elseif isempty(d4)%判断历史闹钟4是否为空,若是则设置主调函数的h 的第2个细胞,否则向下运作a1=4;a2=a1; else a1=5;a2=a1;%否则设置主调函数h第5个细胞内容end while a2>=2%若有新闹钟,则全部历史闹钟都往后移动位置,新闹钟排在主调函数h的第1个细胞内a3=a2; a3=a3-1; {a2}={a3};a2=a2-1; end b1=‘00’;b2=‘00’;b3=‘00’;%以下是填写主调函数h相应的细胞内的内容c=‘-’; if H b1(2)=num2str(H); else b1=num2str(H); end if M b2(2)=num2str(M); else b2=num2str(M); end if S
b3(2)=num2str(S); else b3=num2str(S); 10《数学软件》课程设计end D=[b1,c,b2,c,b3]; {a2}=D; set(,’string’,{a2}) a2=a2+1; if a2 set(,’string’,{a2})a2=a2+1; end if a2 set(,’string’,{a2})a2=a2+1; end if a2 set(,’string’,{a2})a2=a2+1; end set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’) %并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); function off_naozhong_Callback(hObject, eventdata, handles) % hObject handle to off_naozhong (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(_naozhong,’string’,’ ‘); set(_naozhong,’string’,’ ‘);
set(_naozhong,’string’,’ ‘); set(,’BackgroundColor’,’white’) %若按键点击,则关闭闹钟,则图形右上角的图案变白色set(,’string’, ‘闹钟已关闭’)%并且有1秒的中文提示闹钟已关闭pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); 以下是5个历史闹钟显示静态文本框11的编码:function xinnaozhong1_Callback(hObject, eventdata, handles) % hObject handle to xinnaozhong1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a1=get(,’string’);%若按了此按键,则设置此时刻为新闹钟,并且历史闹钟重新排序A1=a1(find(a1>=48&a1 b1=str2num(A1(1));b2=str2num(A1(2));b 3=str2num(A1(3));
b4=str2num(A1(4));b5=str2num(A1(5));b
6=str2num(A1(6)); nzH=b1*10+b2; nzM=b3*10+b4; nzS=b5*10+b6; set(_naozhong,’string’,nzH)
set(_naozhong,’string’,nzM)
set(_naozhong,’string’,nzS)
set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); function xinnaozhong2_Callback(hObject, eventdata, handles) % hObject handle to xinnaozhong2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a1=get(,’string’);%若按了此按键,则设置此时刻为新闹钟,并且历史闹钟重新排序a2=get(,’string’); A2=a2(find(a2>=48&a2
b1=str2num(A2(1));b2=str2num(A2(2));b 3=str2num(A2(3));
b4=str2num(A2(4));b5=str2num(A2(5));b 6=str2num(A2(6)); nzH=b1*10+b2; nzM=b3*10+b4; nzS=b5*10+b6; set(_naozhong,’string’,nzH)
set(_naozhong,’string’,nzM)
set(_naozhong,’string’,nzS) set(,’string’,a2) set(,’string’,a1) 12《数学软件》课程设计set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); function xinnaozhong3_Callback(hObject, eventdata, handles) % hObject handle to xinnaozhong3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see
GUIDATA)
a1=get(,’string’);%若按了此按键,则设置此时刻为新闹钟,并且历史闹钟重新排序a2=get(,’string’); a3=get(,’string’); A3=a3(find(a3>=48&a3 b1=str2num(A3(1));b2=str2num(A3(2));b 3=str2num(A3(3));
b4=str2num(A3(4));b5=str2num(A3(5));b 6=str2num(A3(6)); nzH=b1*10+b2; nzM=b3*10+b4; nzS=b5*10+b6; set(_naozhong,’string’,nzH)
se t(_naozhong,’string’,nzM)
set(_naozhong,’string’,nzS) set(,’string’,a3) set(,’string’,a1) set(,’string’,a2) set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); function xinnaozhong4_Callback(hObject, eventdata, handles) % hObject handle
to xinnaozhong4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a1=get(,’string’);%若按了此按键,则设置此时刻为新闹钟,并且历史闹钟重新排序a2=get(,’string’); a3=get(,’string’); a4=get(,’string’); A4=a4(find(a4>=48&a413《数学软件》课程设计b1=str2num(A4(1));b2=str2num(A4(2));b 3=str2num(A4(3));
b4=str2num(A4(4));b5=str2num(A4(5));b 6=str2num(A4(6)); nzH=b1*10+b2; nzM=b3*10+b4; nzS=b5*10+b6; set(_naozhong,’string’,nzH)
set(_naozhong,’string’,nzM)
set(_naozhong,’string’,nzS) set(,’string’,a4) set(,’string’,a1) set(,’string’,a2) set(,’string’,a3)
set(,’BackgroundColor’,’red’) %若按
键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); function xinnaozhong5_Callback(hObject, eventdata, handles) % hObject handle to xinnaozhong5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a1=get(,’string’);%若按了此按键,则设置此时刻为新闹钟,并且历史闹钟重新排序a2=get(,’string’); a3=get(,’string’); a4=get(,’string’); a5=get(,’string’); A5=a5(find(a5>=48&a5 b1=str2num(A5(1));b2=str2num(A5(2));b 3=str2num(A5(3));
b4=str2num(A5(4));b5=str2num(A5(5));b 6=str2num(A5(6)); nzH=b1*10+b2; nzM=b3*10+b4; nzS=b5*10+b6;
set(_naozhong,’string’,nzH)
set(_naozhong,’string’,nzM)
set(_naozhong,’string’,nzS) set(,’string’,a5) set(,’string’,a1) set(,’string’,a2) set(,’string’,a3) set(,’string’,a4) 14《数学软件》课程设计set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); 编辑完代码后,按保存然后按运行结果如下:
《数学软件》课程设计set(,’BackgroundColor’,’red’) %若按键打开,则图形右上角的图案变红色set(,’string’, ‘闹钟已开启’)%并且提示有1秒提示中文pause(1) set(,’string’,’ ‘)%1秒后结束提示guidata(hObject, handles); 编辑完代码后,按保存然后按运行结果如下:。

相关文档
最新文档