MATLAB: Can some one solved this question, it can’t show the variable y

for loop

clc; clear all; close all;
x=load('X_In.dat','x');
h=load('hn.dat','h');
%%conv
for n=1:x
y(n)=0;
for k=1:n
if(n+k-1>0)
y(n)=y(n)+hk*h(n-k);
else
end
end
end
figure;
subplot(3,1,1); stem(x); xlabel('n');ylabel('x[n]'); grid on;
subplot(3,1,2); stem(h);xlabel('n'); ylabel('h[n]'); grid on;
subplot(3,1,3);stem(y);xlabel('----->n');ylabel('y[n]');grid on;
title('HW2_1a713011');

Best Answer

for n=1:x
This looks suspicious. You loaded the variable x from a file named X_In.dat. Is x scalar? From the fact that you're creating a stem plot from it I'm assuming it's not. In that case this may not do what you think it does. From the documentation for the colon operator: "If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1)." If the first element of the x vector is less than 1, your loops won't execute. How long is the n vector created by the following code? The body of your outermost loop will run that many times.
n = 1:0.5
Looking inside your loop:
if(n+k-1>0)
The smallest value n can have is 1. The smallest value k can have is 1. The smallest value n+k-1 can have is therefore 1+1-1 = 1. This condition is always satisfied.