简体   繁体   English

如何在MATLAB或Octave中的x轴上绘制日期时间

[英]How to plot datetime in x axis in MATLAB or Octave

In MATLAB or Octave, my data has the following format for date/time values: 在MATLAB或Octave中,我的数据具有以下日期/时间值格式:

12:00:34.626 AM 2/26/2017 12:00:34.626 AM 2/26/2017

where it is HH:MM:SS:SSS [A or P]M [M]M/DD/YYYY 在哪里HH:MM:SS:SSS [A或P] M [M] M / DD / YYYY

I want to use it for my x-axis on my plots, and I have tried using datetick , datenum , and datestr and haven't been able to get any of them to work with this format. 我想将其用于绘图上的x轴,并且我尝试使用datetickdatenumdatestr ,但没有任何一种可以使用此格式。

How can I use this formatted value string to label my x-axis datapoints? 如何使用此格式化的值字符串标记我的x轴数据点?

I would use a combination of these functions, namely datnum followed by datetick after plotting. 我将使用这些功能的组合,即在绘制后在datnum之后加上datetick

First off your formating string need to be: 'HH:MM:SS.FFF AM mm/dd/yyyy'. 首先,格式字符串必须为:“ HH:MM:SS.FFF AM mm / dd / yyyy”。

See the following if you need to change format: https://au.mathworks.com/help/releases/R2016b/matlab/ref/datenum.html#inputarg_formatIn 如果您需要更改格式,请参见以下内容: https : //au.mathworks.com/help/releases/R2016b/matlab/ref/datenum.html#inputarg_formatIn

Then use datetick to convert to nice format on plot. 然后使用datetick在图上转换为漂亮的格式。 https://au.mathworks.com/help/releases/R2016b/matlab/ref/datetick.html https://au.mathworks.com/help/releases/R2016b/matlab/ref/datetick.html

So to a single data point= plot you might have: 因此,对于单个数据点=绘图,您可能具有:

xdatestr=['12:00:34.626 AM 2/26/2017'; '12:00:34.626 PM 2/26/2017']
xdatenum=datenum(xdatestr,'HH:MM:SS.FFF AM mm/dd/yyyy')
plot(xdatenum,[0 1])
datetick(gca)

Also note datetime , is whole seperate way of doing it, and uses a different format string convention. 还要注意datetime ,是完全独立的方式,并且使用不同的格式字符串约定。 http://au.mathworks.com/help/matlab/ref/datetime.html http://au.mathworks.com/help/matlab/ref/datetime.html

You can use the following format: 您可以使用以下格式:

d = datetime('12:00:34.626 AM 2/26/2017','InputFormat','hh:mm:ss.SSS a M/dd/yyyy')

result: 结果:

d = 
   26-Feb-2017 00:00:34

and if you want to see also the fractions of a second: 如果您还想看几分之一秒:

>> d.Second
ans =
       34.626

For multiple dates strings, stored in a cell array, just replace the string '12:00:34.626 AM 2/26/2017' above with the cell array. 对于存储在单元格数组中的多个日期字符串,只需将上面的字符串'12:00:34.626 AM 2/26/2017'替换为单元格数组。

Then you just write plot(d,y) (where y is your data) and you get the x-axis in a time format. 然后,您只需编写plot(d,y) (其中y是您的数据),便会以时间格式获得x轴。 You can further customize this format by using the DatetimeTickFormat property: 您可以使用DatetimeTickFormat属性进一步自定义此格式:

plot(d,1,'DatetimeTickFormat','hh:mm:ss.SSS a M/dd/yyyy')

And you will get: 您将获得:

DatetimeTickFormat

see also here and here . 在这里这里也可以看到。

Another example is this. 另一个例子是这样。

date={'12:00:34.600 AM 2/26/2017','12:00:34.700 AM 2/26/2017','12:00:34.800 AM 2/26/2017','12:00:34.900 AM 2/26/2017'};
timeFormat='HH:MM:SS.FFF AM mm/dd/yyyy';
xdatenum=datenum(date,timeFormat);
data=0:3;
plot(xdatenum,data)
datetick('x',timeFormat,'keepticks')
view([-20,90])

I tilted the plot a little to avoid overlap of x ticks. 为了避免x刻度重叠,我将图倾斜了一点。

You can control the number of ticks by using tick properties. 您可以通过使用刻度属性来控制刻度数。

date={'12:00:34.600 AM 2/26/2017','12:00:34.700 AM 2/26/2017','12:00:34.800 AM 2/26/2017','12:00:34.900 AM 2/26/2017'};
timeFormat='HH:MM:SS.FFF AM mm/dd/yyyy';
xdatenum=datenum(date,timeFormat);
data=0:3;
plot(xdatenum,data)
datetick('x',timeFormat,'keepticks')
set(gca,'XTick',[min(xdatenum) max(xdatenum)])
set(gca,'XTickLabel',[date(1),date(end)])

Here, I made just two ticks with minimum date and maximum date. 在这里,我只做了两个最小日期和最大日期的勾选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM