Error using stem()...... X must be same length as Y.

14 views (last 30 days)
clc;
clear all;
close all;
i=-5:0.1:5;
l=length(i);
%unit step signal
for t=1:1
if i(t)<0
u(t)=0;
else
u(t)=1;
end;
end;
figure(1)
subplot(2,1,1);
plot(i,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(i,u);
Error using stem
X must be same length as Y.
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
  5 Comments
KSSV
KSSV on 20 Sep 2023
This is the whole code given by the OP:
clc; clear ;
clc; clear all; close all;
i=-5:0.1:5;
l=length(i);
%unit step signal
for t=1:1
if i(t)<0
u(t)=0;
else
u(t)=1;
end;
end;
figure(1)
subplot(2,1,1);
plot(i,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(i,u(t));
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
% ramp signal
for t=1:1
if i(t)<0
r(t)=0;
else
r(t)=i(t);
end;
end;
figure(2)
subplot(2,1,1);
plot(i,r);
xlabel('time');
ylabel('amplitude');
title('continous time ramp signal');
subplot(2,1,2);
stem(i,r);
xlabel('time');
ylabel('amplitude');
title('disc time unit ramp signal');
%sinusoidal signal
f=0.1;
sinus=sin(2*pi*f*i);
figure(3)
subplot(2,1,1);
plot(i,sinus);
xlabel('time');
ylabel('amplitude');
title('continous time sinusoidal signal');
subplot(2,1,2);
stem(i,sinus);
xlabel('time');
ylabel('amplitude');
title('discrete time sinusoidal signal');
%impulse signal
for t=1:1
if i(t)==0
imp(t)=1;
else
imp(t)=0;
end;
end;
figure(4)
subplot(2,1,1);
plot(i,imp);
xlabel('time');
ylabel('amplitude');
title('continous time impulse signal');
subplot(2,1,2);
stem(i,imp) ;
xlabel('time');
ylabel('amplitude');
title('discrete time impulse signal');
% exponential signal
n=-1:0.1:1;
a=1;
exp11=exp(a*n);
figure(5)
subplot(2,1,1);
plot(n,exp11);
xlabel('time');
ylabel('amplitude');
title('continous time exponential signal');
subplot(2,1,2);
stem(n,exp11);
xlabel('time');
ylabel('amplitude');
title('discrete time exponential signal');
%traingular signal
t=0:0.01:1;
d=0:0.2:1;
y1=pulstran(t,d,'tripuls',0.1);
d=0.1:0.2:1;
y2=pulstran(t,d,'tripuls',0.1);
tri=y2-y1;
figure(6)
subplot(2,1,1);
plot(i,tri);
xlabel('time');
ylabel('amplitude');
title('continous time triangular signal');
subplot(2,1,2);
stem(i,tri);
xlabel('time');
ylabel('amplitude');
title('discrete time triangular signal');
% square signal
i=0:0.1:3;
l=length(i);
f=1;
sq=square(2*pi*f*i);
figure(7)
subplot(2,1,1);
plot(i,sq);
xlabel('t--->');
ylabel('amp --->');
title('continous time square signal');
subplot(2,1,2);
stem(i,sq); xlabel('samples --->');
ylabel('amplitude --->');
title('discrete time square signal');
% sinc function
i=-5:0.1:5;
l=length(i);
ss=sinc(i);
figure(8)
subplot(2,1,1);
plot(i,ss);
xlabel('t--->');
ylabel('amp --->');
title('continous time sinc signal');
subplot(2,1,2);
stem(i,ss);
xlabel('samples --->');
ylabel('amplitude --->');
title('discrete time sincsignal');
% sawtooth waveform
T=10*(1/50);
fs=1000;
dt=1/fs;
t=0:dt:T-dt;
y=2*sawtooth(2*pi*50*t);
figure(1);
subplot(2,1,1);
plot(t,y);
grid on;
subplot(2,1,2);
stem(t,y);
grid on;
Deva
Deva on 20 Sep 2023
Did you get all signals in this program,i didn't getting impulse, triangular and sawtooth signals at a time.

Sign in to comment.

Accepted Answer

Florian Bidaud
Florian Bidaud on 20 Sep 2023
You need to replace for t=1:1 by for t = 1:l
  1 Comment
Deva
Deva on 20 Sep 2023
Edited: David on 1 Oct 2023
clc;
clear all;
close all;
i=-5:0.1:5;
l=length(i);
%unit step signal
for t=1:l
if i(t)<0
u(t)=0;
else
u(t)=1;
end;
end;
figure(1)
subplot(2,1,1);
plot(i,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(i,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
I got the answer 😁tq

Sign in to comment.

More Answers (1)

KSSV
KSSV on 20 Sep 2023
I have given one wproper working code for unit step signal. Understand it and change the others like wise.
t=-5:0.1:5;
nt = length(t) ;
%unit step signal
u = zeros(1,nt) ;
for i=1:nt
if t(i) < 0
u(i)=0;
else
u(i)=1;
end
end
figure(1)
subplot(2,1,1);
plot(t,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(t,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');
You need not to use aloop. Without loop:
t=-5:0.1:5;
nt = length(t) ;
%unit step signal
u = ones(1,nt) ;
u(t<0) = 0 ;
figure(1)
subplot(2,1,1);
plot(t,u);
xlabel('time');
ylabel('amplitude');
title('continous time unit step');
subplot(2,1,2);
stem(t,u);
xlabel('time');
ylabel('amplitude');
title('discrete time unit step');

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!