Subscript indices must either be real positive integers or logicals.

1 view (last 30 days)
Hi everyone, I'm learning Matlab for university. I need your help to understand how to solve this problem. This is my script:
fc=4;
dt=1/fc;
t=0:dt:19-dt;
s=[ones(1,4*fc) zeros(1,6*fc)];
h=sin(2*pi*t2/15 + pi/4);
figure
plot([0:length(s)-1]*dt,s);
figure
plot(t2,h);
l1=length(s);
l2=length(h);
l=l1+l2-1;
S=fft(s,l);
H=fft(h,l);
conv=ifft(S.*H);
figure
plot(conv,'r-');
y=conv(h,s);
figure
plot(y,'b');
the program gives me the error "Subscript indices must either be real positive integers or logicals" in the line "y=conv(h,s)"...but I don't understand why...I'm going mad, please help me!

Answers (2)

Roger Stafford
Roger Stafford on 2 Feb 2014
You have used the term 'conv' two ways, first as an array and second as the convolution function. In the second usage, matlab still thinks it is now an array, so the h and s are misinterpreted as indices. That is probably the source of your error message.
Lesson to be learned: you should never use the name of a matlab function as also the name of something else. It is "sacrosanct".
  2 Comments
Giovanni
Giovanni on 2 Feb 2014
thank you =) but there is still a problem :S I changed the name of my variable and Matlab still continue with the same error :(
Giovanni
Giovanni on 2 Feb 2014
no no no ok my mistake =) i cleared all variables and retried and now it goes well =) thank you very much!! =)

Sign in to comment.


per isakson
per isakson on 2 Feb 2014
Learn how to use the debugger. It's good!
  2 Comments
Giovanni
Giovanni on 2 Feb 2014
unfortunatly I don't have all this time because my exam is in a few days..
Matt J
Matt J on 2 Feb 2014
Edited: Matt J on 2 Feb 2014
It takes no more than an hour to learn. You will waste more time if you don't, since you won't have the know-how to cope with future bugs.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!