Matlab—常用操作之动态绘图

qqAnimation- MATLAB & Simulink

一、animatedline

Create animated line - MATLAB animatedline (mathworks.com)

h = animatedline;
axis([0,4*pi,-1,1])

x = linspace(0,4*pi,100);
y = sin(x);
pic_num=1;
for k = 1:length(x)
    addpoints(h,x(k),y(k));

    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num==1
    imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    elseif mod(pic_num,3)==1
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;

    drawnow limitrate
    
end

 二、沿线条跟踪标记

Trace Marker Along Line- MATLAB & Simulink

x = linspace(0,10,100);
y = sin(x);
plot(x,y)
hold on
p = plot(x(1),y(1),'o','MarkerFaceColor','red');
hold off
axis manual
pic_num=1;
for k = 2:length(x)
    p.XData = x(k);
    p.YData = y(k);
    
    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num==1
    imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    elseif mod(pic_num,3)==1
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;

    drawnow limitrate
end

三、沿着线条移动一组对象

Move Group of Objects Along Line- MATLAB & Simulink

x = linspace(-6,6,100);
y = sin(x);
plot(x,y)
axis manual

ax = gca;
h = hgtransform('Parent',ax);
hold on
plot(x(1),y(1),'o','Parent',h);
hold off
t = text(x(1),y(1),num2str(y(1)),'Parent',h,...
    'VerticalAlignment','top','FontSize',14);

pic_num=1;
for k = 2:length(x)
    m = makehgtform('translate',x(k)-x(1),y(k)-y(1),0);
    h.Matrix = m;
    t.String = num2str(y(k));

     F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num==1
    imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    elseif mod(pic_num,3)==1
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;

    drawnow limitrate
    drawnow
end

四、对图形对象进行动画处理

Animate Graphics Object- MATLAB & Simulink

theta = linspace(-pi,pi);
xc = cos(theta);
yc = -sin(theta);
plot(xc,yc);
axis equal
xt = [-1 0 1 -1];
yt = [0 0 0 0];
hold on
t = area(xt,yt); % initial flat triangle
hold off

pic_num=1;
for j = 1:length(theta)-10
    xt(2) = xc(j); % determine new vertex value
    yt(2) = yc(j);
    t.XData = xt; % update data properties
    t.YData = yt;
    
    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num==1
    imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    elseif mod(pic_num,3)==1
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;

    drawnow limitrate % display updates
end


四、绘制三维箭头

Create Stream Particle Animations- MATLAB & Simulink

vz = 10; % velocity constant
a = -32; % acceleration constant         
t = 0:.1:1;
z = vz*t + 1/2*a*t.^2;

vx = 2;
x = vx*t;

vy = 3;
y = vy*t;

u = gradient(x);
v = gradient(y);
w = gradient(z);
scale = 0;

figure
quiver3(x,y,z,u,v,w,scale)
view([70,18])

五、创建流粒子动画

Create Stream Particle Animations- MATLAB & Simulink

clear all
load wind
[sx sy sz] = meshgrid(100,20:2:50,5);

verts = stream3(x,y,z,u,v,w,sx,sy,sz);
sl = streamline(verts);


view(-10.5,18)
daspect([2 2 0.125])
axis tight;
set(gca,'BoxStyle','full','Box','on')

iverts = interpstreamspeed(x,y,z,u,v,w,verts,0.01);
set(gca,'SortMethod','childorder');
   streamparticles(iverts,15,...
	'Animate',10,...
	'ParticleAlignment','on',...
	'MarkerEdgeColor','none',...
	'MarkerFaceColor','red',...
	'Marker','o');

 六、为曲面添加动画效果

clear all
theta = 0:pi/40:pi;                   % polar angle
phi = 0:pi/20:2*pi;                   % azimuth angle

[phi,theta] = meshgrid(phi,theta);    % define the grid

degree = 6;
order = 1;
amplitude = 0.5;
radius = 5;

Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;

for kk = 2: size(theta,1)
    yy = [yy Ymn];
end

yy = yy.*cos(order*phi);

order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;

r = rho.*sin(theta);    % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = rho.*cos(theta);

figure
s = surf(x,y,z);

light               % add a light
lighting gouraud    % preferred lighting for a curved surface
axis equal off      % set axis equal and remove axis
view(40,30)         % set viewpoint
camzoom(1.5)        % zoom into scene

scale = [linspace(0,1,20) linspace(1,-1,40)];    % surface scaling (0 to 1 to -1)

pic_num=1
for ii = 1:length(scale)

    rho = radius + scale(ii)*amplitude*yy/order;

    r = rho.*sin(theta);
    x = r.*cos(phi);
    y = r.*sin(phi);
    z = rho.*cos(theta);

    s.XData = x;    % replace surface x values
    s.YData = y;    % replace surface y values
    s.ZData = z;    % replace surface z values

    pause(0.05)     % pause to control animation speed

    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num==1
    imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    elseif mod(pic_num,3)==1
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;

end

七、

  • 18
    点赞
  • 200
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值