How to fix the range of x axis while plotting live signals from Arduino to Matlab

9 views (last 30 days)
LuYao Guo
LuYao Guo on 25 Feb 2021
Hi Everyone, I have a question about live plotting from Arduino to Matlab. In the code I attach here, I try to continuously plot the two channels from Arduino to Matlab, but I find that in the plot, the x axis keep increasing thus leads to the compression of plots. Can you please help me to fix the number of x axis so that the x axis is scrolling? Thank you!
clear all
clc
%User Defined Properties
a = arduino('COM5','ProMini328_5v'); % define the Arduino Communication port
plotTitle = 'Arduino Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Voltage'; % y-axis label
legend1 = 'Optical Channel 1';
legend2 = 'Optical Channel 2';
yMax = 40 %y Maximum Value
yMin = 0 %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 40; % set y-max
delay = .01; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = 0;
data1 = 0;
data2 = 0;
count = 0;
%Set up Plot
plotGraph1 = plot(time,data1,'-b')
hold on %hold on makes sure all of the channels are plotted
plotGraph2 = plot(time, data2,'-g' )
title(plotTitle,'FontSize',15);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
legend(legend1,legend2);
axis([yMin yMax min max]);
grid(plotGrid);
tic
while ishandle(plotGraph1) %Loop when Plot is Active will run until plot is closed
dat1 = readVoltage(a,'A2');
dat2 = readVoltage(a,'A3');
count = count + 1;
time(count) = toc;
data1(count) = dat1(1)
data2(count) = dat2(1)
set(plotGraph1,'XData',time,'YData',data1);
set(plotGraph2,'XData',time,'YData',data2);
axis([0 time(count)+10 min max]);
%xlim([max(0,time(count)-0.1) max(10,time(count)-0.1)]);
%ylim(min,max);
%Update the graph
pause(delay);
end
delete(a);

Answers (0)

Community Treasure Hunt

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

Start Hunting!