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

1 回表示 (過去 30 日間)
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

採用された回答

Madhu Govindarajan
Madhu Govindarajan 2018 年 11 月 20 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by