Problem with loop trying to get an ECG in real time.

1 view (last 30 days)
Hi... I have the following Callback, trying to get an ECG signal in a loop for real time. Everything works OK, but suddenly I receive an error "Attempted to access a(1); index out of bounds because numel(a)=0.
Error in MonitorECG>Monitor_Callback (line 175) senal(i)=(a(1)*5/1024)*Amplificacion ; %Guarda la señal original
Error in gui_mainfcn (line 95) feval(varargin{:});"
And this is part of the code that generates troubles.... I dont know what's the problem in the iteration... I've tried to clean the a vector and to declare again in each operation...but that solution does not work... It makes some random values in the graph and then throws the error...
if true
% %Beginning of the code
while(pasada==1) %Este bucle se repite mientras no se presione el botón PARAR.
cla(handles.Pantalla,'reset'); %Limpia la pantalla de AXES
% CONFIGURO LA VENTANA Tiempo Original
axes(handles.Pantalla);
xlabel('Tiempo (s)');
ylabel('Voltaje (V)');
xlim([0 tcaptura]);
ylim([0 5.2])
title('Señal en tiempo real');
%a = axes('XLim',[0 tcaptura],'YLim',[-1 4]); % Esto es el
l1 = line(nan,nan,'Color','r','LineWidth',2);
grid on;
% INICIALIZAR VARIABLES
i = 1; %Contador de muestras por pasada
t = 0;
j = 1; %Contador de muestras para procesamiento
% COMENZAR EL MUESTREO
tic % Pone a andar el cronometro.
while t<tcaptura
t = toc; % Mide el tiempo de la primer muestra.
% leer del puerto serie
a = fscanf(s,'%d');
senal(i)=(a(1)*5/1024)*Amplificacion ; %Guarda la señal original
filtrada(j)=senal(i); % Va copiando los valores de señal en el vector muestra.
Orden=str2num(get(handles.Ord,'String'));
************************ FILTRADO ***************************************
if(j == Orden) %Cuando se almacenan las muestras para el filtro
%COMENZAMOS CON LA FFT
muestras=length(filtrada);
ordenadas1=fft(filtrada,muestras);
ordenadas1=abs(ordenadas1);
abcisas1=linspace(0,rate,muestras);
axes(handles.Espectro);
mode=get(handles.Modo3,'Value');
if(mode==0)
plot(abcisas1, ordenadas1,'r');
else
stem(abcisas1, ordenadas1,'r','MarkerSize',3);
end
%CONFIGURO LA VENTANA Espectro Original
axes(handles.Espectro);
xlabel('Frecuencia (Hz)');
ylabel('Voltaje (V)');
xlim([0 uint8(rate)/2]);
ylim([0 100])
title('Espectro en tiempo real');
%a = axes('XLim',[0 tcaptura],'YLim',[-1 4]); % Esto es el
l2 = line(nan,nan,'Color','r','LineWidth',2);
grid on;
if(get(handles.FiltroDC,'Value')==0) %Si está activo el filtro DC
% % filtrada=filtrada-nivelDC;
% % end;
% end;
tiempo(i)=t;
% Dibujo en la Pantalla de Tiempo Original
axes(handles.Pantalla);
x = linspace(0,i/rate,i);
set(l1,'YData',senal(1:i),'XData',x);
drawnow
% seguir
i = i+1 % Cuenta la cantidad de muestras que va tomando
j=j+1 %Incremento el contador de muestras para filtro.
end;
% Mostrar cronometro para
rate=i/t; %Relacionada con linea 162
fprintf('%g s de captura a %g cap/s \n',t,i/t);
pasada=get(handles.Monitor,'Value');
if(pasada==0)
set(handles.Monitor,'String','MONITOR');
set(handles.Monitor,'BackgroundColor',[0.941 0.941 0.941]);
set(handles.Monitor,'ForegroundColor','black');
end
end
%
end
Thanks in advance!!!

Answers (0)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!