Scrollable Plot in real time from Arduino to Matlab

11 visualizaciones (últimos 30 días)
LuYao Guo
LuYao Guo el 1 de Mzo. de 2021
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);

Respuestas (0)

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by