Multiple animated lines in two different figures plotted simultaneously

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.

 Accepted Answer

Subplot is easy to use:

5 Comments

Is this the right approach? With this, the x axis(time) on the first sub plot is not correct.
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);
ax = gca;
ax.YLim = [0 400];
legend('1','2')
subplot(2,1,2)
h3 = animatedline('Color','r','LineWidth',1);
h4 = animatedline('Color','b','LineWidth',1);
ax = gca;
ax.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
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
ax for subplot(2,1,1) has been overwritten by ax for subplot(2,1,2). So you ended up updating only the lower subplot. You need to update the upper one as well.
How about this:
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('x','keeplimits')
drawnow
end
Thank you very much. This definitely makes a lot of sense. This works but I still have the x-axis on the first subplot as integer values. Did I miss something?
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

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!