Warning: Integer operands are required for colon operator when used as index.

18 vues (au cours des 30 derniers jours)
Mohamed Mahir
Mohamed Mahir le 9 Avr 2021
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]);

Réponses (2)

DGM
DGM le 9 Avr 2021
You need integers to do indexing operations.
plot(f,Ak(1:floor((length(V2)+1)/2))); grid on

Walter Roberson
Walter Roberson le 9 Avr 2021
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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by