Real time plot is very slow when I try to graph 6 subplot

Can you please help me, dear gentlemen: The values are sent through the arduino via a serial monitor. When I try to graph one of the values, they go at the desired speed, but when I want to graph the six values in a different window, it becomes very slow.
There are 6 values that the arduino sends, so be it random values, the same thing happens, please help me
here the code:
delete(instrfind({'Port'},{'COM2'})); %#ok<*INSTRF>
s = serial('COM2','BaudRate',9600,'Terminator','CR/LF'); %#ok<SERIAL>
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
fopen(s);
tmax = 30; % tiempo de captura en s
rate = 15;
% inicializar
a = zeros(1,tmax*rate);
b = zeros(1,tmax*rate);
c = zeros(1,tmax*rate);
d= zeros(1,tmax*rate);
e = zeros(1,tmax*rate);
f = zeros(1,tmax*rate);
i = 2;
t = 0;
tic
while t<tmax
t = toc;
% leer del puerto serie
z = fscanf(s,'%f,%f,%f,%f,%f,%f')';
%asignar el dezplazamiento del eje x
x = linspace(0,i/rate,i);
%asignacion de los variables leidos del puerto serie
a(i)=z(1);
b(i)=z(2);
c(i)=z(3);
d(i)=z(4);
e(i)=z(5);
f(i)=z(6);
%Comienzo de la graficacion de movimiento
subplot(3,2,1)
plot(a(1:i),'XData',x)
title('Cadera Izquierda')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
subplot(3,2,2)
plot(b(1:i),'XData',x)
title('Cadera Derecha')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
subplot(3,2,3)
plot(c(1:i),'XData',x)
title('Rodilla Izquierda')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
subplot(3,2,4)
plot(d(1:i),'XData',x)
title('Rodilla Derecha')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
subplot(3,2,5)
plot(e(1:i),'XData',x)
title('Tobillo Izquierdo')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
subplot(3,2,6)
plot(f(1:i),'XData',x)
title('Tobillo Derecho')
xlabel('Tiempo (s)')
ylabel('Angulo mov.')
grid on
% seguir
i = i+1;
end
clc;

Respuestas (1)

Do not keep calling subplot and so on.
Set up the subplots and titles ahead of time. In each one, call animatedline() and record the handles.
Then as you go through the loop, use addpoints() with the appropriate handle. Remember to call
drawnow limitrate
at the end of the data-adding statements.
a(i)=z(1);
b(i)=z(2);
c(i)=z(3);
d(i)=z(4);
e(i)=z(5);
f(i)=z(6);
Don't do that. You are extending the arrays each cycle through the loop.
The first cycle you create the variables as scalars, copying in the appropriate z values.
The second cycle, you extend the scalars into vectors of length 2. That requires creating vectors of length 2 and copying the scalars into the vector. Amount of data moved and discarded so far is 1 per vector.
The third cycle, you extend the vectors of length 2 into vectors of length 3. That requires creating vectors of length 3 and copying the vectors of length 2 into the vectors of length 3. Amount of data moved and discarded so far is 1 (first round) + 2 (now) = 3
The fourth cycle, you extend the 3 into 4, creating 4, copying 3. Amount of data moved and discarded so far is 1 + 2 + 3 = 6
Fifith cycle... amount of data moved and discarded so far is 1 + 2 + 3 + 4 = 10
By the time you have reached the n'th, you have moved and discarded sum(1:(n-1)) = (n-1)*n/2 . For (say) 1000 iterations that would have wasted 999*1000/2 = 449550 values.
If you use animatedline() then animatedline() keeps track of the data, by default up to 1000000 points (adjustable), but does it in a more efficient manner that requires a lot less copying.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 16 de Jun. de 2023

Respondida:

el 16 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by