Why do I get "Array indices must be positive integers or logical values" error when trying to calculate the average range for the last five observation in line 121?

2 次查看(过去 30 天)
load('ecgClear_1.mat')
x = ecgClear_1(:,1);
Fs = 257;
HF = butter2filtfilt( x, Fs, 20, 'high');
LF = butter2filtfilt( x, Fs, 0.15, 'low');
ecg = x-HF-LF;
figure();
plot(ecg);
%%
% figure()
% plot(x);
ecg_dif = [];
ecg_dif(1:4,1) = 0;
ecg_dif2 = [];
ecg_dif2(1:4,1) = 0;
for n =5:numel(ecg)
ecg_dif(n,1)= abs(ecg(n,1)-ecg(n-2,1));
ecg_dif2(n,1)= abs(ecg(n,1)-2*ecg(n-2,1)+ecg(n-4,1));
end
res = 1.3*ecg_dif+1.1*ecg_dif2;
%%
figure()
a1 = subplot(2,1,1);
plot(ecg);
a2 = subplot(2,1,2);
plot(res);
grid on;
a2.XLim = a1.XLim;
%%
A = 100;
id = res>=A;
fl = false(numel(id),1);
for i = 1:numel(id)-6
s = sum(id(i:i+6));
if s >= 4
fl(i) = true;
end
end
%%
T_Lim = fix(0.2*Fs);
i = 1;
Q_start = [];
while i<=numel(id)
if fl(i)
Q_start =[Q_start;i];
i=i+T_Lim;
else
i = i+1;
end
end
%%
T_Lim = fix(0.2*Fs);
i = 1;
Q_start = [];
R_end = [];
R_peak = [];
R_fl = false;
while i<=numel(id)
if fl(i)
Q_start =[Q_start;i];
[~,mID] = max(abs(ecg(i:i+fix(T_Lim*0.5))));
R_peak = [R_peak; mID+i-1];
R_fl = true;
i=i+T_Lim;
isPlus = ecg(mID+i-1)>0;
for j = mID+i:i+T_Lim
if isPlus && R_fl
if ecg(j)<0
R_end = [R_end ; j];
R_fl = false;
end
elseif ~isPlus && R_fl
if ecg(j)>0
R_end = [R_end;j];
R_fl = false;
end
end
end
if R_fl == true
R_end = [R_end; mID+i-1+mID];
end
i = i+T_Lim;
else
i =i+1;
end
end
%%
figure()
plot(ecg);
hold on;
stem(Q_start,ecg(Q_start));
stem(R_peak,ecg(R_peak));
stem(R_end,ecg(R_end));
grid on
%%
QS = [Q_start R_end];
Data = ecg;
N = size(QS,1);
for i=2:N
RR(i,1) = (R_peak(i)-R_peak(i-1))/Fs;
end
RR(1,1)= 0;
QSparams = struct();
N = size(QS,1);
for i =1:N
Q = QS(i,1);
S = QS(i,2);
QSparams.M(i,1) = mean(ecg(Q:S));
QSparams.std(i,1) = std(ecg(Q:S));
QSparams.max(i,1) = max(ecg(Q:S));
QSparams.min(i,1) = min(ecg(Q:S));
QSparams.range(i,1) = QSparams.max(i,1)- QSparams.min(i,1);
QSparams.L(i,1) = sum(abs(ecg(Q:S)));
QSparams.L2range(i,1) = QSparams.L(i,1)/QSparams.range(i,1);
QSparams.L2time(i,1) = QSparams.L(i,1)/((S-Q)/Fs);
R = QSparams.range;
AR = mean(R(end-5+1:end));
%relR = QSparams.range/AR;
if i~=1 && i~=N
QSparams.minRR(i,1) = min(RR(i,1),RR(i+1,1));
QSparams.minDist(i,1) = min((Q-QS(i-1,2))/Fs,(QS(i+1,1)-S)/Fs);
else
QSparams.minRR(i,1)= 0;
QSparams.minDist(i,1)=0;
end
end
figure()
P1 = QSparams.range;
P2 = QSparams.minRR;
P3 = QSparams.L2time;
scatter3(P1,P2,P3);
QRSstruct_clear = struct('Q_start',Q_start,'R_peak',R_peak,'R_end',R_end);
r = max(QS);

采纳的回答

KSSV
KSSV 2020-11-6
You should know that, the array indices in MATLAB should be positive integers or logicals. If not you will get this error.
Demo:
A = rand(10,1) ;
A(1) % no error, 1 is positive
A(10) % no error 10 is positve
A(0) % error index cannot be 0
A(-1) % error indices cannot be negative
%
idx = A>0.5 ;
A(idx) % no error, logical indexing
Check in your code, where you are going wrong with the indices. Learn about debugging code. Stop at the line, debug and check the indices.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by