Matlab的循环动画 [英] Matlab for loop animations

查看:672
本文介绍了Matlab的循环动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画函数的图形,但我不能让程序绘制正确的点。我想绘制时间0〜10分和动画这个图。我如何获得的情节作为时间的函数?

I'm trying to animate the graph of a function but I cant get the program to graph the correct points. I want to plot points between time 0 and 10 and animate this graph. How do I get the plot as a function of time?

clear;
w = 2*pi; 
t = 0:.01:10;
y = sin(w*t); 
x = cos(w*t);


for  j=1:10
  plot(x(6*j),y(6*j),'*');
  axis square;
  grid on;
  F(j) = getframe;

 end 
 movie(F,1,1);

我精code为:

I refined the code to:

clear;
 w = 2*pi; 



 for  j=2:11
 t=j-1;
 y = sin(w*t); 
 x = cos(w*t);
  plot(x(t),y(t),'*');
  axis square;
  grid on;
  F(j) = getframe;

 end 
 movie(F);

这应该做的但是我想现在我得到一个索引超出矩阵维度。我怎样才能解决这个问题?

This should do what I'm trying however now I'm getting an "Index exceeds matrix dimension." How can I solve this?

推荐答案

下面是显示沿圆弧轨迹动画点,而录制AVI电影的例子。

Here is an example that show an animated point along a circular path, while recording an AVI movie.

要了解更多关于在MATLAB做动画和录像电影,看看这个的指南

To learn more about doing animations and recording movies in MATLAB, check out this guide.

%# some parameters
ERASEMODE = 'normal';        %# normal,xor
RENDERER = 'painters';       %# painters,zbuffer,opengl

%# data
t = linspace(0,2*pi,100)';   %'# adjust number of points here
D = [cos(t) -sin(t)];

%# plot circluar path
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(D(:,1), D(:,2), 'Color','b', 'LineWidth',2)
grid on, axis([-1.5 1.5 -1.5 1.5]), axis square
xlabel('x'), ylabel('y'), title('Circle Animation')
%#set(gca, 'DrawMode','fast')

%# moving point
hPoint = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE,  ...
        'Color','r', 'Marker','.', 'MarkerSize',30);

%# moving coordinates text
hTxtCoords = text(D(1,1), D(1,2), sprintf('(%.2f,%.2f)',D(1,:)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');

%# angle text
hTxtAngle = text(0, 0, num2str(t(1),'%.02f'), ...
    'FontSize',15, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','center', 'VerticalAlignment','middle');

%# prepare video output
useVideoWriter = ~verLessThan('matlab','7.11');
if useVideoWriter
    vid = VideoWriter('vid.avi');
    vidObj.Quality = 100;
    vid.FrameRate = 30;
    open(vid);
else
    vid = avifile('vid.avi', 'fps',30, 'quality',100);
end

%# loop
for i=1:numel(t)
    set(hPoint, 'XData',D(i,1), 'YData',D(i,2))    %# update point location
    set(hTxtAngle, 'String',num2str(t(i),'%.02f')) %# update angle text
    set(hTxtCoords, 'Position',D(i,:), ...         %# update angle text
        'String',sprintf('(%.3f,%.3f)',D(i,:)))
    drawnow                                        %# force refresh
    if ~ishandle(hPoint), break; end               %# if you close the figure

    %# capture frame
    if useVideoWriter
        writeVideo(vid,getframe);
    else
        vid = addframe(vid, getframe(gcf));
    end 
end

%# close and save video output
if useVideoWriter
    close(vid);
else
    vid = close(vid);
end

%# open AVI file using system default player
winopen('vid.avi')

这篇关于Matlab的循环动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆