⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animate.m

📁 很多matlab的源代码
💻 M
字号:
function animate(t,y,mode)
% ANIMATE Animated plot of functions (For MATLAB versions 4.x only)
% 
%       ANIMATE(T,Y) plots Y vs array T
%	ANIMATE(Y) plots Y vs index starting with 0,1,..
%
%       NOTE: For M functions, Y is an MxN matrix with N=length(T) 
%	
%       The user can click on the following Push Buttons:
%           Start - starts animation
%           Stop  - stops animation
%           Step  - steps through animation one point at a time
%
%       ANIMATE (with NO INPUT ARGUMENTS) invokes the following example:
%
%	% Animate three functions on the same set of axes.
%         >>t = 0:0.01:1;
%         >>animate(t,[sin(2*pi*t);cos(2*pi*t);exp(-2*t)])
%


% ADSP Toolbox: Version 2.0 
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998


global STOP
  clr=['cmyrgb'];

if nargin == 0,
 help animate
 disp('Strike a key to see results of example')
 pause
 t=0:0.01:1;
 y = [sin(2*pi*t);cos(2*pi*t);exp(-2*t)];
 animate(t,y);
 return
end

if nargin < 3, % setup environment

 if nargin == 1, 
  y = t;
  [r,c] = size(y);
  t = 1:c;
 end

 [m1,n1]=size(t);
 if m1 == 1,
  lt=n1;
 else
  lt=m1;
 end

 [m,n]=size(y);
 if m == lt,
  y=y.';
  m=n;
 end

% setup the figure window
 p=[min(t) max(t) min(min(y)) max(max(y))];
 h=ishold;
 if h==0,
  clf
  hold on
  axis(p);
 end
 set(gca,'box','on','pos',[0.13,0.15,0.775,0.807])
 grid on
 
% setup the handle graphics objects
 for j=1:m,
  q = rem(j,6)+1;
%  if q == 4, q = 7; end
%  q = ['c' int2str(q)];
   q=clr(j);
  plt(j) = plot(t(1),y(j,1),'.','color',q,'erase','xor','markersize',20);
 end
 hold off;
v=matverch;
if v < 5, yes='yes';no='no';else,yes='on';no='off';end
 
 btn(1) = uicontrol('style','push','string','Start','pos',[10,10,60,20],...
                    'callback','animate([],[],1)','interruptible',yes);
 btn(2) = uicontrol('style','push','string','Stop','pos',[75,10,60,20],...
                    'callback','animate([],[],2)','enable','off');
 btn(3) = uicontrol('style','push','string','Step','pos',[140,10,60,20],...
                    'callback','animate([],[],3)');

 set(btn,'units','norm');
 set(btn(1),'userdata',t);
 set(btn(2),'userdata',y);
 set(btn(3),'userdata',plt);
 set(gcf,'userdata',btn);
 set(gca,'userdata',1);
 
% run animation first time
 animate([],[],4);
 
elseif any(mode == [1,4]), % start/first time animation

 STOP = 0;
 
% obtain handle and plot information
 btn = get(gcf,'userdata');
 t = get(btn(1),'userdata');
 y = get(btn(2),'userdata');
 data = get(btn(3),'userdata');
 begin = get(gca,'userdata');
 [m,n] = size(y);
 plt = data(1:m);

 if mode == 1,
  set(btn(2),'enable','on');
 end
 set(btn([1,3]),'enable','off');
 
 hold on;
 if begin == 1,
  if length(data) > m,
   set(data(m+1:2*m),'erase','xor');
  end
  for j = 1:m,
   q = rem(j,6)+1;
%   if q == 4, q = 7; end
%   q = ['c' int2str(q)];
    q=clr(j);
   lin(j) = line('color',q,'erase','none','xdata',[],'ydata',[]);
  end
  set(btn(3),'userdata',[plt,lin]);
 else
  lin = data(m+1:2*m);
 end
 hold off;
 
 for k = begin:n,
  for j = 1:m
   set(plt(j),'xdata',t(k),'ydata',y(j,k))
   set(lin(j),'xdata',t(1:k),'ydata',y(j,1:k));
  end
  if STOP,
   set(gca,'userdata',k);
   break;
  end
  drawnow
 end

 if k == n, % animation ran to completion
  set(gca,'userdata',1);
  set(btn(2),'enable','off');
  set(btn([1,3]),'enable','on');
 end
  
elseif mode == 2, % stop animation

% obtain handle information
 btn = get(gcf,'userdata');

 set(btn(2),'enable','off');
 set(btn([1,3]),'enable','on');
 STOP = 1;
 
elseif mode == 3, % step through animation

% obtain handle and plot information
 btn = get(gcf,'userdata');
 t = get(btn(1),'userdata');
 y = get(btn(2),'userdata');
 data = get(btn(3),'userdata');
 begin = get(gca,'userdata');
 [m,n] = size(y);
 plt = data(1:m);

 hold on;
 if begin == 1,
  if length(data) > m,
   set(data(m+1:2*m),'erase','xor');
  end
  for j = 1:m,
   q = rem(j,6)+1;
%   if q == 4, q = 7; end
%   q = ['c' int2str(q)];
    q=clr(j);
   lin(j) = line('color',q,'erase','none','xdata',[],'ydata',[]);
  end
  set(btn(3),'userdata',[plt,lin]);
 else
  lin = data(m+1:2*m);
 end
 hold off;
  
 begin = begin + 1;
 for j = 1:m
  set(plt(j),'xdata',t(begin),'ydata',y(j,begin))
  set(lin(j),'xdata',t(1:begin),'ydata',y(j,1:begin));
 end

 if begin == n,
  set(gca,'userdata',1);
 else
  set(gca,'userdata',begin);
 end

end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -