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

4 views (last 30 days)
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 (1)

Priyank Pandey
Priyank Pandey on 20 Dec 2022
Hi LuYao,
I understand you want to know how to fix the range of X axis while plotting a live signal from Arduino to MATLAB.
You can refer to the following document, this is an example for how to plot live data of temperature sensor from Arduino to MATLAB:
This will help you better understand how to fix an axis while plotting live signal on MATLAB.
Regards
Priyank

Categories

Find more on Instrument Control Toolbox 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!