首页 > 解决方案 > 如何在 MATLAB 中绘制日期时间的差异?

问题描述

我在不同时间测量了值,我想绘制它们。在 excel 文件中,列的时间和值是分开的。时间格式为:HH:MM:SS 例如:11:20:21。我想在 x 轴上绘制时间之间的差异(以秒为单位)。y 轴将是测量值。我从 excel 文件中读取数据,但不知道如何绘制 x 轴。例如:excel 中的数据:10:20:33,10:20:35,10:20:39 和 x 轴上的值是:0,2,4(以秒为单位)。这是我的开始:

V1= xlsread('measured.xlsx','D4:D5295')
V2= xlsread('measured.xlsx','E4:E5295')
CV= xlsread('measured.xlsx','F4:F5295')
t=xlsread('measured.xlsx','B4:B5295')

plot(t,V1,t,V2,t,CV)
 datetick('x',13)
title('Measurement')
ylabel('Data [V]')
xlabel('time [s]')

有了这个,我得到了 x 轴日期,但我想得到两个测量时间之间经过的时间。

标签: matlabdatetime

解决方案


我要做的是将时间向量转换为“datatime”数据类型,计算时间之间的差异(使用 diff 命令),然后将其转换为秒。

V1= xlsread('measured.xlsx','D4:D5295')
V2= xlsread('measured.xlsx','E4:E5295')
CV= xlsread('measured.xlsx','F4:F5295')
t=xlsread('measured.xlsx','B4:B5295')
% convert it to datatime data type
t = datetime(t); 
% Get the difference between times and add the "0" to keep 
%   the same vector size.
t = [0; seconds(diff(t))];

plot(t,V1,t,V2,t,CV)
 datetick('x',13)
title('Measurement')
ylabel('Data [V]')
xlabel('time [s]')

推荐阅读