How to change multiple animatedline() axes?
7 views (last 30 days)
Show older comments
Hey everyone!
Ive been working on a live data plotter that reads data from my serial port and plots the data live using the animatedline() function and it works (sometimes it lags, and it doesnt like a plot size too big, but it works)
Im having trouble getting all the plots (6 plots in total) to all have moving axes. I have a "subplot" function that reads the serial port and plots to each of the subplots, but only the last plot has a moving axis and the others are fixed. I have included a picture
I couldn't find any documentation online on how ot defined which axis goes to which animatedline() so I figured I could ask here as I have come up with no luck. If you have any questions feel free to ask. For context I have a microcontroller sending wireless serial data to my computer. Its estimating attitude, altitude, and whatnot.
Thanks!!
clear all;
delete(instrfindall);
clear;
close all;
clc;
s = serial('COM11', 'BaudRate',115200);
fopen(s);
t = 0;
y = 1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
TIME(y,1) = str2double(data(1));
PHI(y,1) = str2double(data(4));
THETA(y,1) = str2double(data(5));
PSI(y,1) = str2double(data(6));
P(y,1) = str2double(data(7));
Q(y,1) = str2double(data(8));
R(y,1) = str2double(data(9));
AX(y,1) = str2double(data(10));
AY(y,1) = str2double(data(11));
AZ(y,1) = str2double(data(12));
ALT(y,1) = str2double(data(13));
IAS(y,1) = str2double(data(14));
figure('Name', 'Sensor Data');
set(gcf, 'Position', [100 100 800 800])
subplot(6,1,1:2)
h1 = animatedline(TIME(y,1), PHI(y,1),'Color','r');
h2 = animatedline(TIME(y,1), THETA(y,1));
h3 = animatedline(TIME(y,1), PSI(y,1),'Color','b');
xlim([TIME(y,1) TIME(y,1)+20]);
ylim([-100 100]);
grid on
ylabel("\Phi \Theta \Psi [deg]");
subplot(6,1,3)
h4 = animatedline(TIME(y,1), P(y,1),'Color','r');
h5 = animatedline(TIME(y,1), Q(y,1));
h6 = animatedline(TIME(y,1), R(y,1),'Color','b');
xlim([TIME(y,1) TIME(y,1)+20]);
ylim([-200 200]);
grid on
ylabel("\omega_b [deg/s]");
subplot(6,1,4)
h7 = animatedline(TIME(y,1), AX(y,1),'Color','r');
h8 = animatedline(TIME(y,1), AY(y,1));
h9 = animatedline(TIME(y,1), AZ(y,1),'Color','b');
xlim([TIME(y,1) TIME(y,1)+20]);
ylim([-2 2]);
grid on
ylabel("a_b [g]");
subplot(6,1,5)
h10 = animatedline(TIME(y,1), ALT(y,1),'Color','b');
xlim([TIME(y,1) TIME(y,1)+20]);
grid on
ylabel("P_D [m]");
subplot(6,1,6)
h11 = animatedline(TIME(y,1), IAS(y,1),'Color','b');
xlim([TIME(y,1) TIME(y,1)+20]);
ylim([-10 80]);
grid on
xlabel("Time [sec]");
ylabel("V_a [mph]");
tic
while t <= 20
y = y+1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
TIME(y,1) = str2double(data(1));
PHI(y,1) = str2double(data(4));
THETA(y,1) = str2double(data(5));
PSI(y,1) = str2double(data(6));
P(y,1) = str2double(data(7));
Q(y,1) = str2double(data(8));
R(y,1) = str2double(data(9));
AX(y,1) = str2double(data(10));
AY(y,1) = str2double(data(11));
AZ(y,1) = str2double(data(12));
ALT(y,1) = str2double(data(13));
IAS(y,1) = str2double(data(14));
addpoints(h1, TIME(y,1), PHI(y,1));
addpoints(h2, TIME(y,1), THETA(y,1));
addpoints(h3, TIME(y,1), PSI(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+1])
addpoints(h4, TIME(y,1), P(y,1));
addpoints(h5, TIME(y,1), Q(y,1));
addpoints(h6, TIME(y,1), R(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+1])
addpoints(h7, TIME(y,1), AX(y,1));
addpoints(h8, TIME(y,1), AY(y,1));
addpoints(h9, TIME(y,1), AZ(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+1])
addpoints(h10, TIME(y,1), ALT(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+1])
addpoints(h11, TIME(y,1), IAS(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+1]) % Only "xlim" that performs the moving graph
t=t+0.05;
drawnow
end
toc
fclose(s);
delete(s);
clear s;
Pay no attention to the data value.
Its hard to tell, but the first 4 graphs are rigidly plotted, and only the last and final graph Va actually has a moving graph. Hope this illustrates the problem adequately
![Capture.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230268/Capture.jpeg)
0 Comments
Answers (1)
Walter Roberson
on 18 Jul 2019
animatedline defaults to being associated with the current axes at the time it is executed. Your code for that is okay.
The problem with your code is that xlim defaults to being associated with the current axes at the time the xlim is executed. The last current axes is your last subplot() so you are continually updating just that one.
You should take a copy of the handle produced by each subplot call. Put them into a vector. Now make a single set() call passing in the vector of axes and 'Xlim' and the new values. All of the axes would be affected at the same time with no need for individual xlim calls.
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!