《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 vindex
set(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 vfname
try
len=length(vfname);
run(vfname(1:len-2));
catch
msgbox('sorry, there are some error while playing movie!','NOTICING');
end
以下是其中两个动画程序
snow动画
%***************设置背景****************************%
A=imread('H:\YOU.jpg');
h=figure;
imshow(A);
axes
axis([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:N
handles(i)=text(x(1,i),x(2,i),'*','fontsize',fontsize(i),'color','w');
end
while 1
if ~ishandle(h)
return
end
for i=1:N
temp=get(handles(i),'position');
step=get(handles(i),'fontsize')/48*0.05;
if temp(2)<0
new_handles_N=new_handles_N+1;
new_handles(new_handles_N)= copyobj(handles(i),gca);
if new_handles_N==500
delete(new_handles);
new_handles_N=0;
end
temp(1)=rand(1);
temp(2)=1;
else
temp(1)=temp(1)+rand(1)*0.1-0.05;
temp(2)=temp(2)-step;
end
set(handles(i),'position',temp,'rotation',get(handles(i),'rotation')+5);
end
pause(.2)
end
简单程序动画
try
Movie=moviein(16);
for k=1:16
plot(fft(eye(k+16)))
axis equal
axis off
M(k)=getframe;
end
movie(M,30);
catch
msgbox('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 symPause
try
if 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')
end
catch
msgbox('sorry, there are some error while excuting the
function!','NOTICING','error');
end
function timerPause(hObject,eventdata,handles)
global symPause
symPause=1;
while symPause==1
pause(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) try
if get(handles.txtvword,'string')
set(handles.stxtshow,'string',get(handles.txtvword,'string'),...
'visible','on','fontname','??ì?','fontsize',12.0);
else
set(handles.stxtshow,'visible','off');
msgbox('Add some words first!');
end
catch
msgbox('sorry, there are some error while excuting the
function!','NOTICING','error');
end
2)音乐模块
(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 mfilepath
try
[mfname mpname mindex]=uigetfile('*.wav','choose music');
if mindex
mfilepath=[mpname mfname];
set(handles.edit1,'string',mfilepath);
end
catch
msgbox('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 sign
sign='false';
try
if mfilepath~=0
[data,Fs,nbits]=wavread(mfilepath);
else
try
[data,Fs,nbits]=wavread('you are a song in my heart.wav');
catch
msgbox('sorry,the default music does not exist','NOTICING');
end
end
player=audioplayer(data,Fs,nbits);
play(player);
sign='ture';
set(handles.btnmpause,'string','pause','Enable','on');
catch
msgbox('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 sign
try
if 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');
end
catch
msgbox('sorry, there are some error while excuting the
function!','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 sign
try
stop(player);
sign='false';
set(handles.btnmpause,'string','pause','Enable','off');
catch
msgbox('ERROR','NOTICING');
end
3)其他辅助功能
(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 sign
try
if strcmp(sign,'true')
stop(player);
close all
end
catch
msgbox('sorry, there are some error while excuting the
function!','NOTICING','error');
end
(2)异常捕捉机制try-catch
由于程序功能再设计上考虑的并不是十分全面,可能会出现预料不到的错误,因此加入此功能,避免在出现未知错误时,程序崩溃。
4、结果分析
功能实现上看,基本上能达到预期效果,各个功能均可正常执行,在动画播放方面,有些方面还不尽如人意,比如说,由于实现了动画的可切换性,也就是说动画程序的独立性,使得自己编写的m文件和程序内部参数部分不一致,使得出现不同效果,比如,有时动画在设计好的GUI界面上显示,有时又以独立的窗口运行。音乐模块则与预期一致。另外,应该说是非人为因素,在动画播放的时候,添加背景音乐,此时动画会暂停,当音乐开始播放时,动画也恢复正常。从这个结果看,程序内部命令会相互影响,所以这将是我们队优化程序的一个方向。
5、改进方向
从程序演示过程发现以下问题,这将是本程序优化的方向。
(1)回调函数之间的干扰:最明显的结果表现为,动画运行过程中,点击其他的按钮,将短时间暂停动画程序。所以如何将动画独立于GUI之外,同时又受GUI界面按钮的控制,这是程序优化重点之一。
(2)动画程序编写模块:虽说本程序可以实现动画程序的自由选择,但原则上还是选择了.m文件,并且在程序内部的参数不知晓的情况下编写,程序实现的结果可能不一致。
(3)动画模块完全是由程序文件执行的,如何能够动态的将动画录制并保存,后者说直接播放.avi格式的动画,将是僵尸程序优化的特色之一。
(4)动画播放过程中,未能直接终止动画程序。
(5)动画并行播放问题。可以让多个动画程序同时打开,但只有一个动画播放,其他的被暂停,等其结束后,其他的又可以继续执行。所以当要切换动画时,如何自动停止已经播放的动画,有待优化。
6、心得体会
通过此次的maltab课设,加强了运用matlab编程的能力。一方面是GUI设计,另一方面则是代码编写。而程序的实现不仅可以通过GUI写入代码,还可以直接通过代码绘制图形,控件,以及callback函数等等。因为完成此次课设是从不懂到学会的过程,期间查阅了很多书籍,同时在网上也借阅了他人的经验成果,达到了编程质的飞跃。然而在编程的过程,曲折是明显的,调试程序时,警告时不时地出现,从而了解了各类经常出现的错误。
其次,由于matlab更多的是直接调用内部命令,所以说如果不清楚内部命令的话,编程则显得很吃力,先动画音乐程序就是,大部分是调用命令。虽然说本程序主题代码并不是非常的长,却要求了较高的基础。了解命令后,则又显得很简单。但是像动画程序,大部分则是自己编写算法,更显自主性,这部分更能显示个人编程能力以及严谨性。
文献
[1] MATLAB GUI设计学习手记第二版罗华飞北京航天航空大学出版社
[2]精通matlab+GUI设计第2版陈垚光电子工业出版社
[3]MATLAB程序设计教程李海涛邓樱高等教育出版社