当前位置:文档之家› MATLAB程序设计报告--基于MATLAB动画播放及音乐播放

MATLAB程序设计报告--基于MATLAB动画播放及音乐播放

《MATLAB程序设计》课程设计报告设计题目:基于MATLAB的动画演示及背景音乐插入专业:2011级通信工程姓名(学号):储兆雄1162310213邓少林1162310214徐凯越1162310223指导教师:倪建军(博士/副教授)时间:2013年12月20日目录1、设计目的2、总体设计3、具体设计(功能实现)4、结果分析5、改进方向6、心得体会文献附录1、设计目的学会运用matlab工具箱实现matlab GUI设计,处理动画运行,以及添加背景音乐,并实现其动态操作,如继续、暂停等功能。

2、总体设计主要包括:动画模块,音乐模块,动画显示模块3、具体设计(功能实现)1) 动画模块(1)打开动画文件:从文件打开对话框选择动画程序,实现动画播放的可选择性程序实现代码如下:function btnvopen_Callback(hObject, eventdata, handles)% hObject handle to btnvopen (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global vfname %动画文件名[vfname vpname vindex]=uigetfile('*.m','choose moive file');len=length(vfname);if vindexset(handles.txtvname,'string',vfname(1:len-2))end打开对话框效果如下:(2)开始动画播放:先获取文件名称vfname(1:len-2),然后用run()命令来执行程序实现代码如下:function btnvstart_Callback(hObject, eventdata, handles)% hObject handle to btnvstart (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global vfnametrylen=length(vfname);run(vfname(1:len-2));catchmsgbox('sorry, there are some error while playing movie!','NOTICING');end以下是其中两个动画程序snow动画%***************设置背景****************************%A=imread('H:\YOU.jpg');h=figure;imshow(A);axesaxis([0 1 0 1]);axis off;set(gcf,'color','k');N=20;handles=zeros(1,N);x=rand(2,N);fontsize=floor(rand(1,N)*17+32);new_handles_N=0;%绘图for i=1:Nhandles(i)=text(x(1,i),x(2,i),'*','fontsize',fontsize(i),'color','w');endwhile 1if ~ishandle(h)returnendfor i=1:Ntemp=get(handles(i),'position');step=get(handles(i),'fontsize')/48*0.05;if temp(2)<0new_handles_N=new_handles_N+1;new_handles(new_handles_N)= copyobj(handles(i),gca);if new_handles_N==500delete(new_handles);new_handles_N=0;endtemp(1)=rand(1);temp(2)=1;elsetemp(1)=temp(1)+rand(1)*0.1-0.05;temp(2)=temp(2)-step;endset(handles(i),'position',temp,'rotation',get(handles(i),'rotation')+5);endpause(.2)end简单程序动画tryMovie=moviein(16);for k=1:16plot(fft(eye(k+16)))axis equalaxis offM(k)=getframe;endmovie(M,30);catchmsgbox('sorry, there are some error while playing movie!','NOTICING');end动画效果如下:(3)暂停/继续功能:获取pause按钮string参数,实现暂停和继续播放功能的切换,而通过timerPause()函数来实现其功能程序实现代码如下:function btnvpause_Callback(hObject, eventdata, handles)% hObject handle to btnvpause (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global symPausetryif strcmp(get(handles.btnvpause,'string'),'pause')handles.timer1 = timer('Period', 0.1, 'ExecutionMode','singleShot','timerFcn', {@timerPause});set(handles.btnvpause,'string','continue')start(handles.timer1);elseif strcmp(get(handles.btnvpause,'string'),'continue')symPause =0;set(handles.btnvpause,'string','pause')endcatchmsgbox('sorry, there are some error while excuting thefunction!','NOTICING','error');endfunction timerPause(hObject,eventdata,handles)global symPausesymPause=1;while symPause==1pause(1)end(4)添加文字function btnvshow_Callback(hObject, eventdata, handles)% hObject handle to btnvshow (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tryif get(handles.txtvword,'string')set(handles.stxtshow,'string',get(handles.txtvword,'string'),...'visible','on','fontname','¿¬Ìå','fontsize',12.0);elseset(handles.stxtshow,'visible','off');msgbox('Add some words first!');endcatchmsgbox('sorry, there are some error while excuting thefunction!','NOTICING','error');end2)音乐模块(1)打开音乐function btnmbrowse_Callback(hObject, eventdata, handles)% hObject handle to btnmbrowse (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global mfilepathtry[mfname mpname mindex]=uigetfile('*.wav','choose music');if mindexmfilepath=[mpname mfname];set(handles.edit1,'string',mfilepath);endcatchmsgbox('Error,while importing music£¡','NOTICING','error'); end(2)播放音乐function btnstart_Callback(hObject, eventdata, handles)% hObject handle to btnstart (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global mfilepath player signsign='false';tryif mfilepath~=0[data,Fs,nbits]=wavread(mfilepath);elsetry[data,Fs,nbits]=wavread('you are a song in my heart.wav');catchmsgbox('sorry,the default music does not exist','NOTICING');endendplayer=audioplayer(data,Fs,nbits);play(player);sign='ture';set(handles.btnmpause,'string','pause','Enable','on');catchmsgbox('Rrror while playing music!','NOTICING');end(3)暂停/继续功能function btnmpause_Callback(hObject, eventdata, handles)% hObject handle to btnmpause (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global player signtryif strcmp(get(handles.btnmpause,'string'),'pause')pause(player);set(handles.btnmpause,'string','continue');elseif strcmp(get(handles.btnmpause,'string'),'continue')resume(player);set(handles.btnmpause,'string','pause');endcatchmsgbox('sorry, there are some error while excuting thefunction!','NOTICING');end(4)停止播放音乐function btnmstop_Callback(hObject, eventdata, handles)% hObject handle to btnmstop (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global player signtrystop(player);sign='false';set(handles.btnmpause,'string','pause','Enable','off');catchmsgbox('ERROR','NOTICING');end3)其他辅助功能(1)关闭程序:在退出程序时,音乐不会自动停止,所以为了在退出程序时同时关闭音乐,才加入此功能function figure1_DeleteFcn(hObject, eventdata, handles)% hObject handle to figure1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global player signtryif strcmp(sign,'true')stop(player);close allendcatchmsgbox('sorry, there are some error while excuting thefunction!','NOTICING','error');end(2)异常捕捉机制try-catch由于程序功能再设计上考虑的并不是十分全面,可能会出现预料不到的错误,因此加入此功能,避免在出现未知错误时,程序崩溃。

相关主题