Effacer les filtres
Effacer les filtres

Multiple animated lines in two different figures plotted simultaneously

21 vues (au cours des 30 derniers jours)
Hello,
I am currently using a self built 3D-printed pressure sensor to get realtime reading in terms of change in the capacitance. The sensor has two different sensing areas. I currently have a code which displays the pressure from those two sensing areas, as shown in the figure below.
I would like to have another figure along with this, which display load values from the same sensor simultaneously. So, two figures, one as the same figure below and another figure with converted load values displayed simultaneously. Instead of two figures, a subplot would also work I suppose. The entire code is in a while loop. I tried the following:
figure(1)
h = animatedline('Color','r','LineWidth',1);
h2 = animatedline('Color','b','LineWidth',1);
figure(2)
h3 = animatedline('Color','g','LineWidth',1);
h4 = animatedline('Color','m','LineWidth',1);
ax = gca;
ax.YGrid = 'on';
ax.YLim = [-200 400];
legend('1','2')
while ~stop
% After reading the input from the sensor
addpoints(h,datenum(t),senleft)
addpoints(h2,datenum(t),senright)
addpoints(h3,datenum(t),strru1)
addpoints(h4,datenum(t),strrd1)
end
Thank you in advance. Please let me know if you need any further information.

Réponse acceptée

Yongjian Feng
Yongjian Feng le 3 Nov 2021
Subplot is easy to use:
  5 commentaires
Yongjian Feng
Yongjian Feng le 3 Nov 2021
I see.
datetick can take axis_handle as the first input paramter.
clear all
close all
s=serialport('COM7',9600);
configureTerminator(s,"CR/LF")
sub1=subplot(2,1,1);
h = animatedline('Color','r','LineWidth',1);
h2 = animatedline('Color','b','LineWidth',1);
ax1 = gca; % NEED TO DISTINGUISH UPPER SUBPLOT AND LOWER ONE
ax1.YLim = [0 400];
legend('1','2')
subplot(2,1,2)
h3 = animatedline('Color','r','LineWidth',1);
h4 = animatedline('Color','b','LineWidth',1);
ax2 = gca; % NEED TO DISTINGUISH UPPER SUBPLOT AND LOWER ONE
ax2.YLim = [-1000 2000];
legend('3','4')
startTime = datetime('now');
while ~stop
str = readline(s);
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),senleft)
addpoints(h2,datenum(t),senright)
addpoints(h3,datenum(t),strlu)
addpoints(h4,datenum(t),strld)
i=i+1;
% Update axes
ax1.XLim = datenum([t-seconds(15) t]); % UPDATE UPPER. Is this what you want?
ax2.XLim = datenum([t-seconds(15) t]); % UPDATE LOWER
datetick(ax1, 'x','keeplimits') % UPPER
datetick(ax2, 'x','keeplimits') % LOWER
drawnow
end
Suraj Giri
Suraj Giri le 3 Nov 2021
Thank you. Works perfectly!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by