
應(yīng)同學(xué)之托第一次嘗試用matlab做動(dòng)畫,弄了一下午加一晚上終于弱弱地搞定了。
先做了個(gè)GUI界面,點(diǎn)擊Debug —run就會(huì)跳出個(gè)對(duì)話框,然后輸入?yún)?shù),如果參數(shù)是負(fù)數(shù)的話就跳出警告窗口。如下圖:


然后就開始動(dòng)畫了:

下邊是源文件:
function varargout = mmmove(varargin)
% MMMOVE M-file for mmmove.fig
% MMMOVE, by itself, creates a new MMMOVE or raises the existing
% singleton*.
%
% H = MMMOVE returns the handle to a new MMMOVE or the handle to
% the existing singleton*.
%
% MMMOVE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MMMOVE.M with the given input arguments.
%
% MMMOVE('Property','Value',...) creates a new MMMOVE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before mmmove_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to mmmove_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 mmmove
% Last Modified by GUIDE v2.5 04-Apr-2008 21:41:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mmmove_OpeningFcn, ...
'gui_OutputFcn', @mmmove_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 mmmove is made visible.
function mmmove_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 mmmove (see VARARGIN)
% Choose default command line output for mmmove
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mmmove wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = mmmove_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 Debug_Callback(hObject, eventdata, handles)
% hObject handle to Debug (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function run_Callback(h, eventdata, handles)
% hObject handle to run (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
prompt={'請(qǐng)輸入速度v:','請(qǐng)輸入長(zhǎng)度L:','請(qǐng)輸入半徑r:'};
defans={'5','10','2'};
v=5;L=10;r=2;
p=inputdlg(prompt,'Input',1,defans);
v=str2double(p(1));
L=str2double(p(2));
r=str2double(p(3));
%guidata(h,handles)
if v<=0|L<=0|r<=0
warndlg('非法輸入','警告')
else
ox1=r;oy1=r;ox2=r+L;oy2=r;
x1=r:0.015*v:r+L;
y1=2*r*ones(size(x1));
thita=0:0.015*v/r:pi;
x2=sin(thita)*r+ox2;
y2=cos(thita)*r+oy2;
x3=r+L:-0.015*v:r;
y3=zeros(size(x3));
x4=-sin(thita)*r+ox1;
y4=-cos(thita)*r+oy1;
x=[x1 x2 x3 x4 x1(1)];
y=[y1 y2 y3 y4 y1(1)];
plot(x,y);
axis([0,2*r+L,-1,2*r+1]);
h_text=text(0,-2,['L = ' num2str(L) ' , ' 'r = ' num2str(r) ' , ' 'v = ' num2str(v)]);
set(h_text,'fontSize',13);
axis equal
set(gca,'Visible','off')
hm=line(r,2*r,'color','red','marker','.','markersize',37,'erasemode','xor');
while 1
for i=1:length(x)
set(hm,'xdata',x(i),'ydata',y(i));
pause(0.0003)
drawnow
end
end
end
為了能讓小球不停的循環(huán)運(yùn)動(dòng),我加了while 1的語句。
這樣在關(guān)閉窗口的時(shí)候,matlab的命令窗里就會(huì)跳出一堆錯(cuò)誤,大概是因?yàn)閒or循環(huán)里涉及到的hm隨著figure的關(guān)閉刪除了,但是程序仍然在運(yùn)行,所以跳出東西說hm是invalid的。是不是可以放個(gè)停止程序的函數(shù)放figure或者axis的deleteFcn里頭,不過沒找到那樣的函數(shù)。
期待好心大牛指教。。。
還有就是速度控制問題,用計(jì)算的的速度控制,運(yùn)行明顯偏慢,大概是程序自己運(yùn)行也耗時(shí)吧,這個(gè)也不知道怎么辦。不過后來看到篇文章說用timer對(duì)象來產(chǎn)生動(dòng)畫的,很不錯(cuò)的樣子。
先引用下來:
(本文為L(zhǎng)iuxqsmile原創(chuàng),轉(zhuǎn)載請(qǐng)保留原文鏈接)
MATLAB中提供了一些動(dòng)態(tài)顯示圖形的例子,例如vibes、truss等,但他們的程序結(jié)構(gòu)都和由GUIDE產(chǎn)生的M文件的結(jié)構(gòu)不同。truss中用while循環(huán)來更新圖形窗口,pause函數(shù)來控制更新的速度。這樣的結(jié)構(gòu)是不適合放在某一個(gè)子函數(shù)中來執(zhí)行的,否則程序在執(zhí)行該子函數(shù)時(shí)無法響應(yīng)用戶的其它操作,只能一開始就設(shè)置終止條件,等待動(dòng)畫的自然結(jié)束。
MATLAB中的定時(shí)器timer可以定時(shí)觸發(fā),周期性地執(zhí)行指定的函數(shù),我們可以據(jù)此來實(shí)現(xiàn)繪圖的自動(dòng)更新,而不會(huì)影響整個(gè)GUI對(duì)用戶其它操作的響應(yīng)。
比如我們要讓一個(gè)曲面隨時(shí)間周期性地變形,類似vibes中的情形,按下“開始”按鈕后,曲面開始周期振動(dòng),同時(shí)我們可以調(diào)整振幅大小、周期的長(zhǎng)短。所不同的是我們一切都在子函數(shù)中完成。
在GUIDE中建立界面元素后,在“開始”按鈕的callback中建立一個(gè)定時(shí)器timer對(duì)象object
handles.timer = timer('Period',0.05,'ExecutionMode','FixedRate',
... 'TimerFcn',{@PlotUpdate,handles});
Period是觸發(fā)的周期,這里設(shè)置為0.05s,ExecutionMode是執(zhí)行的方式,可以有三種選擇,對(duì)于不需要精確控制時(shí)序的動(dòng)畫的影響不大,TimerFcn指定觸發(fā)時(shí)所執(zhí)行函數(shù)的句柄,在這里我們建立一個(gè)函數(shù)PlotUpdate來執(zhí)行繪圖命令,handles作為參數(shù)傳遞到PlotUpdate中去。
注意PlotUpdate的定義:
function PlotUpdate(obj,events,handles)
前兩個(gè)參數(shù)是必不可少的,最后的handles才是用戶傳遞的數(shù)據(jù)。在這個(gè)函數(shù)中你基本上可以無視前兩個(gè)參數(shù),按照曲面的運(yùn)動(dòng)趨勢(shì)產(chǎn)生新的頂點(diǎn)坐標(biāo),然后用set命令將新的坐標(biāo)賦給曲面(面片patch)對(duì)象的Vertices屬性。drawnow更新就可以了。你可以用曲面對(duì)象的UserData屬性存放當(dāng)前振動(dòng)的幅值相位等參數(shù)。
然后在“停止”按鈕的callback中stop(handles.timer)停止定時(shí)器,就可以停止自動(dòng)繪圖了。
在“增大振幅”按鈕的callback中對(duì)振幅參數(shù)作修改。
排除了一些小問題后,你的曲面終于可以動(dòng)起來了,按“增大振幅”等也沒有影響圖形的運(yùn)動(dòng),真的是“一點(diǎn)影響都沒有”??!
問題出在哪里呢?原來,當(dāng)定時(shí)器第一次觸發(fā)時(shí),用戶指定的參數(shù)被傳遞到響應(yīng)函數(shù)中執(zhí)行,以后每一次觸發(fā),都是使用的這同一組參數(shù),并不會(huì)隨著程序的運(yùn)行而更新。好了,現(xiàn)在可以在
“增大振幅”中重新賦一次參數(shù)
set(handles.timer,'TimerFcn',{@PlotUpdate,handles});
怎么樣,達(dá)到你的要求了吧!想知道更多?MATLAB Help的index中輸入timer,夠多了吧。
動(dòng)畫下載:下載后,用matlab7.0以上的版本可運(yùn)行。