So I want to create an animated plot of a discrete-time complex exponential function. The simplest non-animated plot would be given by this:
n=-5:40;
x=(exp((3*4j)*n)).*(n>=0);
y=real(x);
subplot(2,1,1);
stem (n,y)
z=imag(x);
subplot(2,1,2);
stem (n,z)
How do I animate it to show the function for the different numbers of samples considered in a given interval (assuming I have a time interval specified by start second and end second and a vector containing the number of sample values in the given interval)?
I tried along these lines:
figure,hold on
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
stem (k,y) %// Choose your own marker here
pause(0.001);
end
That doesn't compile. How to achieve this?
Short answer:
Make the following two changes:
① Replace xlim([min(x(:)) max(x(:))])
with this xlim([1 numel(x)])
.
② Replace stem (k,y)
with this: stem (k,y(k))
.
Detailed Answer:
① xlim([min(x(:)) max(x(:))])
is giving you the following error:
Error using matlab.graphics.axis.Axes/set
While setting the 'XLim' property of 'Axes':
This is not a valid LimitsWithInfs value. Complex inputs are not supported
The error message tells you exactly what the problem is. Your vector x
contains complex numbers. Time axis having complex numbers also doesn't imply anything.
It seems that you would want to replace this line xlim([min(x(:)) max(x(:))])
with this: xlim([1 numel(x)])
.
② Inside the loop, stem (k,y)
is giving you this error:
Error using stem (line 46)
X must be same length as Y.
The error message tells you exactly what the problem is. Here k
is just a scalar (1x1) but y
is a 1x46 vector.
Since you want to plot y
point by point, replace stem (k,y)
with this: stem (k,y(k))
.
Output after making the mentioned fixes: