Clear Filters
Clear Filters

Scrollable Plot in real time from Arduino to Matlab

20 views (last 30 days)
LuYao Guo
LuYao Guo on 1 Mar 2021
Edited: Zuber Khan on 17 Jul 2024 at 9:18
Hi,
I meet a problem when plotting data from Arduino to Matlab, where the x axis is increasing continously and cause the compression of plot. I want to let the x axis auto-scroll, something like axis(t,t+dx,y1,y2). I've tried two ways plotting the data, shown below. Is there any other methods to make the x axis auto-scrolling? Thank you in advance!
Method 1
%Method 1
clear all
a = arduino('COM5','ProMini328_5v');
%interv = 10;
passo = 1;
t=1;
x=0;
%while(t<interv)
while(t)
b=a.readVoltage('A3');
x=[x,b];
plot(x);
%axis([0,interv,0,5]);
ylim('auto');
grid
t=t+passo;
drawnow;
end
Method 2
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
%plotGraph = plot(time,data,'-r' ) % every AnalogRead needs to be on its own Plotgraph
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
%dat = a.analogRead(0)* 0.48875855327; %Data from the arduino
%dat1 = a.analogRead(2)* 0.48875855327;
%dat2 = a.analogRead(3)* 0.48875855327;
dat1 = readVoltage(a,'A2');
dat2 = readVoltage(a,'A3');
count = count + 1;
time(count) = toc;
%data(count) = dat(1);
data1(count) = dat1(1)
data2(count) = dat2(1)
%This is the magic code
%Using plot will slow down the sampling time.. At times to over 20
%seconds per sample!
%set(plotGraph,'XData',time,'YData',data);
set(plotGraph1,'XData',time,'YData',data1);
set(plotGraph2,'XData',time,'YData',data2);
%x = linspace(0,100,10000);
xlim([0 30]);
%xticks('auto');
ylim('auto');
%axis([time(count) time(count)+10 min max]);
%axis([0 time(count) 1 5]);
%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)

Zuber Khan
Zuber Khan on 17 Jul 2024 at 9:11
Edited: Zuber Khan on 17 Jul 2024 at 9:18
Hi,
It is true that the plot gets compressed as more data points are added to it if the x-axis limits always start from the same origin point.
Another way in which you can update your code can be as follows to make the x-axis auto scrolling:
clear all
a = arduino('COM5','ProMini328_5v');
passo = 1;
t = 1;
x = 0;
scrollWindow = 100; % Define the number of points to display in the plot window
figure;
hPlot = plot(x);
ylim('auto');
grid on;
while true
b = a.readVoltage('A3');
x = [x, b];
% Update plot data
set(hPlot, 'YData', x, 'XData', 1:length(x));
% Update x-axis limits for auto-scrolling
if length(x) > scrollWindow
xlim([length(x)-scrollWindow, length(x)]);
else
xlim([0, scrollWindow]);
end
drawnow;
t = t + passo;
end
The reason for updating 'xlim' to start from 'length(x) - scrollWindow' rather than a fixed point is to create an auto-scrolling effect. This ensures that the plot window always shows the most recent data points, thereby, avoiding compression of plot.
For more information regarding the "set" function, kindly refer to the following documentation link:
Also, further information related to various line properties which can be set using the "set" function can be found here:
Hope this helps!
Regards,
Zuber

Categories

Find more on Graphics Object Properties 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!