Subscript indices must either be real positive integers or logicals.
    1 回表示 (過去 30 日間)
  
       古いコメントを表示
    
As i call the function given below I get some errors:
- undefined function or variable len.
- undefined function or variable maxloc.
- Subscript indices must either be real positive integers or logicals.
the code is as below:
function [hrv, R_t, R_amp, R_index, S_t, S_amp]  = rpeakdetect(data,samp_freq,thresh,testmode)
%%%%%%%%%%%make threshold default 0.2 -> this was 0.15 on MIT data 
if nargin < 4
   testmode = 0;
end
%%%%%%%%%%%make threshold default 0.2 -> this was 0.15 on MIT data 
if nargin < 3
   thresh = 0.2;
end
%%%%%%%%%%%make sample frequency default 256 Hz 
if nargin < 2
   samp_freq = 256;
   if(testmode==1)
       fprintf('Assuming sampling frequency of %iHz\n',samp_freq);
   end
end
%%%%%%%%%%%check format of data %%%%%%%%%%
[a b] = size(data);
if(a>b)
 len =a;
end
if(b>a)
 len =b;
end
%%%%%%%%%%if there's no time axis - make one 
if (a | b == 1);
% make time axis 
  tt = 1/samp_freq:1/samp_freq:ceil(len/samp_freq);
  t = tt(1:len);
  x = data;
end
%%%%%%%%%%check if data is in columns or rows
if (a == 2) 
  x=data(:,1);
  t=data(:,2); 
end
if (b == 2)
  t=data(:,1);
  x=data(:,2); 
end
%%%%%%%%%bandpass filter data - assume 256hz data %%%%%
 % remove mean
 x = x-mean(x);
   % FIR filtering stage
   bpf=x; %Initialise
  if( (samp_freq==128) & (exist('filterECG128Hz')~=0) )
          bpf = filterECG128Hz(x); 
  end
  if( (samp_freq==256) & (exist('filterECG256Hz')~=0) )
          bpf = filterECG256Hz(x); 
  end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%differentiate data %%%%%%%%%%%%%%%%%%%%%%%%%%%
 dff = diff(bpf);  % now it's one datum shorter than before
%%%%%%%%%square data    %%%%%%%%%%%%%%%%%%%%%%%%%%%
 sqr = dff.*dff;   %
 len = len-1; % how long is the new vector now? 
%%%%%%%%%integrate data over window 'd' %%%%%%%%%%%%%%%%%%%%%%%%%
 d=[1 1 1 1 1 1 1]; % window size - intialise
 if (samp_freq>=256) % adapt for higher sampling rates
   d = [ones(1,round(7*samp_freq/256))]; 
 end
 % integrate
 mdfint = medfilt1(filter(d,1,sqr),10);
 % remove filter delay for scanning back through ECG
 delay = ceil(length(d)/2);
 mdfint = mdfint(delay:length(mdfint));
%%%%%%%%%segment search area %%%%%%%%%%%%%%%%%%%%%%%
 %%%%first find the highest bumps in the data %%%%%%
 max_h = max (mdfint(round(len/4):round(3*len/4)));
   %%%%then build an array of segments to look in %%%%%
   %thresh = 0.2;
   poss_reg = mdfint>(thresh*max_h);
%%%%%%%%%and find peaks %%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%find indices into boudaries of each segment %%%
 left  = find(diff([0 poss_reg'])==1); % remember to zero pad at start
 right = find(diff([poss_reg' 0])==-1); % remember to zero pad at end
   %%%%loop through all possibilities  
   for(i=1:length(left))
      [maxval(i) maxloc(i)] = max( bpf(left(i):right(i)) );
      [minval(i) minloc(i)] = min( bpf(left(i):right(i)) );
      maxloc(i) = maxloc(i)-1+left(i); % add offset of present location
      minloc(i) = minloc(i)-1+left(i); % add offset of present location
   end
   R_index = maxloc;
   R_t   = logical(t(maxloc));
   R_amp = maxval;
   S_amp = minval;   %%%%Assuming the S-wave is the lowest
                     %%%%amp in the given window
   S_t   = t(minloc);
%%%%%%%%%%check for lead inversion %%%%%%%%%%%%%%%%%%%
 % i.e. do minima precede maxima?
 if (minloc(length(minloc))<maxloc(length(minloc))) 
  R_t   = t(minloc);
  R_amp = minval;
  S_t   = t(maxloc);
  S_amp = maxval;
 end
%%%%%%%%%%%%
hrv  = diff(R_t);
resp = R_amp-S_amp; 
%%%%%%%%%%%%%%%%%%%%
if (testmode~=0)
figure(1)
hold off
subplot(4,1,1)
plot(t,x);hold on;plot(t,bpf,'r')
title('raw ECG (blue) and zero-pahse FIR filtered ECG (red)')
ylabel('ECG')
hold off;
subplot(4,1,2)
plot(t(1:length(mdfint)),mdfint);hold on;
%plot(t(1:length(sqr)),sqr);hold on;
plot(t,max(mdfint)*bpf/(2*max(bpf)),'r')
plot(t(left),mdfint(left),'og')
plot(t(right),mdfint(right),'om')
title('Integrated data with scan boundaries over scaled ECG')
ylabel('Int ECG')
hold off;
subplot(4,1,3)
plot(t,bpf,'r');hold on;
plot(R_t,R_amp,'+k');
plot(S_t,S_amp,'+g');
title('ECG with R-peaks (black) and S-points (green) over ECG')
ylabel('ECG+R+S')
hold off;
subplot(4,1,4)
hold off
plot(R_t(1:length(hrv)),hrv,'r+')
hold on
title('RR intervals')
ylabel('RR (s)')
hold off
fprintf('Press any key for next block of data\n');
pause
end
please someone help me as its my MS thesis code and I have to submit my thesis just after 02 weeks.
0 件のコメント
回答 (1 件)
  Walter Roberson
      
      
 2015 年 8 月 11 日
        Your code does not define len if a == b -- that is, if data is square. Consider using
len = max(a,b);
Note that the meaning of
if (a | b == 1);
is that the test is to be considered true if b == 1 or if a is non-zero. If you want to test that a is 1 or b is 1 then you need to test that specifically:
if a == 1 | b == 1
You should also be considering your tests for the case where data is empty.
Your code only defines maxloc and related values if
     for(i=1:length(left))
executes at least one cycle. If left is empty, the body of the loop never executes and the variables do not get assigned a value.
For the subscript error, you need to fix the above first and then run and include the complete traceback of the error indicating the line it is occurring on.
0 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Time-Frequency Analysis についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

