Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 view (last 30 days)
I'm trying to compute ZCR(zero crossing rate) of EMG signal. I import csv file as 12000X1 array. I'm using 200 ms analysis window with 50 ms window increment (with a sampling frequency = 4000 Hz). But an error message popped out:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
[fileName, pathName] = uigetfile('*.csv');
data = csvread(fullfile(pathName,fileName)) ;
f1 = bandpass(data,[20 450],4000);
f2 = bandstop(f1,[49 51],4000);
ZCR_array = zeros(225,1);
b = 1;
s = 0;
for d=1:225
for e=1:798
if (f2(b,1) > 0 && f2(b+1,1) < 0)||(f2(b,1) < 0 && f2(b+1,1) > 0)
s = s + 1;
else
break;
end
b = b + 1;
end
m = s / 800;
s = 0;
b = b - 748;
MAV_array(d,1) = m;
end
% I tried another way and it still doesn't work.
for d=1:225
for e=1:799
if ( f2(b,1) * f2(b+1,1) ) < 0
s = s + 1;
else
break;
end
b = b + 1;
b1 = b1 + 1;
end
m = s / 800;
s = 0;
b = b - 749;
MAV_array(d,1) = m;
end

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 28 May 2020
Edited: Srivardhan Gadila on 28 May 2020
for e=1:798
if (f2(b,1) > 0 && f2(b+1,1) < 0)||(f2(b,1) < 0 && f2(b+1,1) > 0)
s = s + 1;
else
break;
end
b = b + 1;
end
Beacause of the break statement in the above code, it can happen that the for loop may terminate for the iteration e <749 which implies that the value of b will be less than 749. Then after the execution of the following code:
m = s / 800;
s = 0;
b = b - 748;
MAV_array(d,1) = m;
the value of b will be less than 1. Hence in the next iteration of d, first iteration of e: f2 is indexed with a non-positive integer b and could be the cause of the error "Index in position 1 is invalid. Array indices must be positive integers or logical values".
I would recommend you to check whether the for loop is terminated for the values of e <749 by displaying the value of e like something as follows:
for e=1:798
if (f2(b,1) > 0 && f2(b+1,1) < 0)||(f2(b,1) < 0 && f2(b+1,1) > 0)
s = s + 1;
else
e
break;
end
b = b + 1;
end

Community Treasure Hunt

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

Start Hunting!