I encounter error 'Array indices must be positive integers or logical values.' in a for loop
Show older comments
Hi,
I was trying to demodulate an ASK signal when I encountered this problem. There is no variables which shares a name with a function. Please help
Code:
%generating ASK signal
%Written By Debagnik Kar 1804373
clc
clear all
close all
time = linspace(0,2,5000)
am = input('Enter amplitude of message signal: ')
fm = input('Enter frequency of message signal: ')
ac = input('Enter amplitude of carrier signal (more than message): ')
fc = input('Enter frequency of carrier signal (more than message): ')
data = am*(square(2*pi*fm*time)+1)
carrier = ac*cos(2*pi*fc*time)
signal = data.*carrier
subplot 311
plot(time, data)
title('message signal')
xlabel('Time')
ylabel('Magnitude')
subplot 312
plot(time, carrier)
title('carrier signal')
xlabel('Time')
ylabel('Magnitude')
subplot 313
plot(time, signal)
title('Modulated signal')
xlabel('Time')
ylabel('Magnitude')
x = abs(signal)
for n = 0:length(signal)
if x>0
d(n) = 1
else
d(n) = 0
end
end
figure(2)
subplot 211
plot(time, data)
subplot 212
plot(time,d)
Error:
Array indices must be positive integers or logical values.
Error in ask (line 45)
d(n) = 0
I am also providing the inputs I have given if that helps.
Inputs:
am = 1
ac = 3
fm = 2
fc = 40
Answers (1)
Star Strider
on 3 Oct 2020
This is the problem:
for n = 0:length(signal)
since 0 is not a positive integer. Positive integers are integers greater than 0, since 0 has no sign.
Categories
Find more on Programming 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!