Warning: Integer operands are required for colon operator when used as index.
Show older comments
Ak=2*abs(fft(V2))/length(V2);Ak(1)=Ak(1)/2;
f=[0:1:(length(V2)-1)/2]*Fs/length(V2);
figure();
plot(f,Ak(1:(length(V2)+1)/2));grid on %(This line is causing the problem)
title('Spectrum for tone');
ylabel('Amplitude');
xlabel('frequency (Hz)');
axis([500 2000 0 1]);
Answers (2)
DGM
on 9 Apr 2021
You need integers to do indexing operations.
plot(f,Ak(1:floor((length(V2)+1)/2))); grid on
Walter Roberson
on 9 Apr 2021
0 votes
If length(V2) is even, then length(V2)-1 is odd, and length(V2)-1)/2 would not be an integer when you do the 0:1: operation. Likewise, if length(V2) is even, then length(V2)+1 is odd and (length(V2)+1)/2 would not be an integer when you do the 1: operation.
Categories
Find more on Matrix Indexing 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!