MATLAB Answers

0

How to make an animated stairstep graph?

Asked by Tai Jia Xun on 14 Jun 2018
Latest activity Commented on by Tai Jia Xun on 16 Jun 2018

I have a stairstep graph (squarewave) plotted from an array, is it possible to make the graph plot in motion?

%%my code: x=[0:1:10]; a=[0 1 0 1 0 1 0 0 1 1 0]; stairs(x,a)

  0 Comments

Sign in to comment.

1 Answer

Answer by jonas
on 14 Jun 2018
Edited by jonas
on 14 Jun 2018
 Accepted Answer

You can use the comet() function if you manipulate the data a bit.

x=[0:1:10]; 
a=[0 1 0 1 0 1 0 0 1 1 0];
xq=0:0.01:10;
aq = interp1(x,a,xq,'previous');
comet(xq,aq)

There is also a similar function called animatedline() which I have never tested. Or as a third, perhaps simplest solution, plot point-by-point in a for-loop using pause(t) in every loop.

  3 Comments

Hi Jones! It's different but it works! thanks you very much! However, is there any way I can change the line color???

It does not appear to be possible. Option two works fine though:

x=[0:1:10]; 
a=[0 1 0 1 0 1 0 0 1 1 0];

%Increase resolution (step size determines speed of animation)

xq=0:0.0001:10;
aq = interp1(x,a,xq,'previous');
figure;
h=animatedline

%Change properties

set(h,'color','r','linewidth',2)
axis([0 10 0 1])
for k = 1:length(aq) 
      addpoints(h,xq(k),aq(k)) 
      drawnow update 
end 

Thank you very much Jonas!!!!

Sign in to comment.