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

23 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
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!