Matlab的一个小动画(带源文件)



应同学之托第一次尝试用matlab做动画,弄了一下午加一晚上终于弱弱地搞定了。
先做了个GUI界面,点击Debug —run就会跳出个对话框,然后输入参数,如果参数是负数的话就跳出警告窗口。如下图:




          然后就开始动画了:


下边是源文件:
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={'请输入速度v:','请输入长度L:','请输入半径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

为了能让小球不停的循环运动,我加了while 1的语句。
这样在关闭窗口的时候,matlab的命令窗里就会跳出一堆错误,大概是因为for循环里涉及到的hm随着figure的关闭删除了,但是程序仍然在运行,所以跳出东西说hm是invalid的。是不是可以放个停止程序的函数放figure或者axis的deleteFcn里头,不过没找到那样的函数。
期待好心大牛指教。。。
还有就是速度控制问题,用计算的的速度控制,运行明显偏慢,大概是程序自己运行也耗时吧,这个也不知道怎么办。不过后来看到篇文章说用timer对象来产生动画的,很不错的样子。
先引用下来:

MATLAB GUI中利用定时器制作动画

(本文为Liuxqsmile原创,转载请保留原文链接
MATLAB中提供了一些动态显示图形的例子,例如vibes、truss等,但他们的程序结构都和由GUIDE产生的M文件的结构不同。truss中用while循环来更新图形窗口,pause函数来控制更新的速度。这样的结构是不适合放在某一个子函数中来执行的,否则程序在执行该子函数时无法响应用户的其它操作,只能一开始就设置终止条件,等待动画的自然结束。
MATLAB中的定时器timer可以定时触发,周期性地执行指定的函数,我们可以据此来实现绘图的自动更新,而不会影响整个GUI对用户其它操作的响应。
比如我们要让一个曲面随时间周期性地变形,类似vibes中的情形,按下“开始”按钮后,曲面开始周期振动,同时我们可以调整振幅大小、周期的长短。所不同的是我们一切都在子函数中完成。
在GUIDE中建立界面元素后,在“开始”按钮的callback中建立一个定时器timer对象object
handles.timer = timer('Period',0.05,'ExecutionMode','FixedRate',
... 'TimerFcn',{@PlotUpdate,handles});
Period是触发的周期,这里设置为0.05s,ExecutionMode是执行的方式,可以有三种选择,对于不需要精确控制时序的动画的影响不大,TimerFcn指定触发时所执行函数的句柄,在这里我们建立一个函数PlotUpdate来执行绘图命令,handles作为参数传递到PlotUpdate中去。
注意PlotUpdate的定义:
function PlotUpdate(obj,events,handles)
前两个参数是必不可少的,最后的handles才是用户传递的数据。在这个函数中你基本上可以无视前两个参数,按照曲面的运动趋势产生新的顶点坐标,然后用set命令将新的坐标赋给曲面(面片patch)对象的Vertices属性。drawnow更新就可以了。你可以用曲面对象的UserData属性存放当前振动的幅值相位等参数。
然后在“停止”按钮的callback中stop(handles.timer)停止定时器,就可以停止自动绘图了。
在“增大振幅”按钮的callback中对振幅参数作修改。
排除了一些小问题后,你的曲面终于可以动起来了,按“增大振幅”等也没有影响图形的运动,真的是“一点影响都没有”啊!
问题出在哪里呢?原来,当定时器第一次触发时,用户指定的参数被传递到响应函数中执行,以后每一次触发,都是使用的这同一组参数,并不会随着程序的运行而更新。好了,现在可以在
“增大振幅”中重新赋一次参数
set(handles.timer,'TimerFcn',{@PlotUpdate,handles});
怎么样,达到你的要求了吧!想知道更多?MATLAB Help的index中输入timer,够多了吧。



动画下载下载后,用matlab7.0以上的版本可运行。

posted on 2008-04-05 02:59 Amigo 阅读(4726) 评论(6)  编辑 收藏 引用

评论

# re: Matlab的一个小动画 2008-04-09 21:50 刘峰

可以通过设置handle graphic 的EraseMode来实现动画。  回复  更多评论   

# re: Matlab的一个小动画 2008-04-11 16:35 Amigo

恩,俺就是用erasemode做的,可是上边那个程序在结束运行的时候会报错,是不是因为我用了死循环的关系?@刘峰
  回复  更多评论   

# re: Matlab的一个小动画 2008-09-23 17:12 hello

怎么将上述源文件copy到matlab编辑器中按博主提供的做法,运行不了?
??? Error using ==> load
Unable to read file mmmove.fig: No such file or directory.

Error in ==> hgload at 43
fileVars = load(filename,'-mat');

Error in ==> openfig at 72
[fig, savedvisible] = hgload(filename, struct('Visible','off'));

Error in ==> gui_mainfcn>local_openfig at 286
gui_hFigure = openfig(name, singleton, visible);

Error in ==> gui_mainfcn at 159
gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt,
gui_Visible);

Error in ==> mmmove at 42
gui_mainfcn(gui_State, varargin{:});

??? Error using ==> load
Unable to read file mmmove.fig: No such file or directory.

Error in ==> hgload at 43
fileVars = load(filename,'-mat');

Error in ==> openfig at 72
[fig, savedvisible] = hgload(filename, struct('Visible','off'));

Error in ==> gui_mainfcn>local_openfig at 286
gui_hFigure = openfig(name, singleton, visible);

Error in ==> gui_mainfcn at 159
gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt,
gui_Visible);

Error in ==> mmmove at 42
gui_mainfcn(gui_State, varargin{:});
  回复  更多评论   

# re: Matlab的一个小动画 2008-09-26 22:55 Amigo

在建立GUI的时候,很多会生成很多源代码,在里边修改就行了~@hello
  回复  更多评论   

# re: Matlab的一个小动画 2008-09-26 23:06 Amigo

我已经将matlab中的两个文件上传,放在文章底部,有需要可以下载。  回复  更多评论   

# re: Matlab的一个小动画(带源文件) 2010-06-16 23:06 eri

hao
very good  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(4)

随笔档案

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜