How to add system date & time in x axis of a plot, corresponding to a real-time measurement

1 visualizzazione (ultimi 30 giorni)
Hi,
Im using an arduino to send some measurments to matlab using usb. As you can see on the code below, im making a plot of that data. I use real-time drawing of the plot adding each new measurement instantly. Measurements appear on the y axis while the number of each measurement appears on the x axis. I want to keep the number of each measurement in x (because this way i can control how many points will appear on the plot). Although, i need to make the system date and time (dd.mm hh:mm) corresponding to each measurement appear instead of the numbers, somehow using xticks i guess. I'm having some trouble doing this. Thanks in advance!
Heres my code:
clear all
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
clc
baud = 115200;
arduino=serial('COM23','BaudRate',baud);
fopen(arduino);
x=linspace(1,1000000,1000000);
y=zeros(1,1000000);
i=0;
j=1;
points=60;
y1=0;y2=17000000;
while (true)
i = i+1;
if (i > points)
j = i - points;
end
y(i) = fscanf(arduino, '%d');
plot(x,y,'-r','LineWidth',1.5)
axis([x(j) x(i+1) y1 y2])
title('Plot','FontSize',16,'Color','r')
grid on
grid minor
ylabel('Measurement','FontSize',12,'FontWeight','Bold')
xlabel('# of Measurement','FontSize',12,'FontWeight','Bold')
drawnow
end

Risposta accettata

Madhu Govindarajan
Madhu Govindarajan il 20 Nov 2018
You can add system date using datetime and datetick. In the example below, I executed datetime(now) at different times which would be the case in your example. You will probably want to execute it right after you do fscanf and just append to your x.
t = datetime(now)
t(2) = datetime('now')
t(3) = datetime('now')
y = [1 2 3]
plot(t,y)
datetick
HTH,
Madhu

Più risposte (0)

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by