Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB已退休,不会更新。

为日期 /时间数据设置新的时间基础

最近,我正在与某人合作,他们拥有一些以各种格式的时间数据,并试图以有意义的方式合并它们。

Contents

问题设置

我制作了一小部分数据并将其加载。

loadSettimeBasiswhos
名称大小字节类属性mydatestr 1x6 12 char t 5x1 161 dateTime

First I looked at the both variables.

mydatestr
MyDatestr = 090506
t
t = 12:32:28 12:59:50 12:59:51 12:59:52 12:59:53

You can see that I have a string representing a date (May 6, 2009), and then adatetime数组进来t. Though I saw thatt曾经是一个datetime, I kept thinking of it as a duration since I didn't see a month, year, etc. And I have to confess, though I usually do check the documentation, I was really resisting it yesterday - not sure why - perhaps I was being more obstinate than usual :-) !

什么不起作用

Next I converted the base date todatetime,并尝试将其添加到t.

d = datetime('090506',“ inputformat','MMddyy')tryd + t抓住E disp(E.message)end
d = 05-Sep-2006在DateTime数组之间未定义添加。

第一次工作镜头

好的,这不起作用。我终于意识到了这一点t仅显示小时,分钟和几秒钟,因为那是datetime数组,而不是一个duration, I had to deal with that! I also realized that I used the wrong formatting for converting the original date formydatestrdatetime呢What was I thinking!

newdate = datetime(mydatestr,“ inputformat','yymmdd')
newdate = 2009年6月6日

Let's inspect the first time point now.

t1 = t(1); [year(t1) month(t1) day(t1)]
ans = 2016 6 7

啊!现在我需要转换日期(不是时间)值t对此老时呢我可以通过计算日期差来做到这一点,从而导致durationarray which I能够添加到基本日期。

OldDate = DateTime(年(T1),月(T1),Day(T1));日期= newdate -olddate newt = t + netatediff
日期= -62136:00:00 newt = 12:32:28 12:59:50 12:59:51 12:59:52 12:59:53:53

让我们检查一下纽特now (I know,眼(纽特)呢)

newt1 = newt(1);newt1.format ='YY-MM-DD HH:MM:SS'
newt1 = 09-05-06 12:32:28

更好的答案

坦率地说,我把它上班了,但它确实很丑陋且有些扭曲。如果我以前负责任地阅读文档,我可能会找到一种更好的方法。我将不立即向您展示DOC,而是向您展示另一种戳戳的方式,以获取更多信息。让我们找出我能做什么datetimearray.

方法(t)
Methods for class datetime: between hour le reshape caldiff interp1 length second cat intersect linspace setdiff cellstr isbetween lt setxor char iscolumn max size colon isdst mean sort ctranspose isempty median sortrows datenum isequal min std dateshift isequaln minus timeofday datestr isfinite minute transpose datetime isinf mode tzoffset datevecismatrix月工会日ismember ndims独特的diff isnat ne vertcat eq isrow numel numel周exceltime isscalar permutime isscalar permute年ge isort ge issort ge issort ymd gt gt gt iSvector yyyymmdd hms isweek isweek isweek posix posixtime horzcattime horzcat horzcat horzcat juliandate季度静态方法:setdefault static default静态方法:setDddddddddddddddddefault静态方法:

Hmmmm!timeofday听起来很有趣,不是!?!因此,让我们再试一次。

newdate = datetime(mydatestr,“ inputformat','yymmdd') tod = timeofday(t) newt2 = newdate + tod whos
newdate = 2009年6月6日tod = 12:32:28 12:59:50 12:59:51 12:59:52 12:59:53 newt2 = 06-May-2009 12:32:28 06-May-2009 12:59:50 06-May-2009 12:59:51 06-May-2009 12:59:52 06-May-2009 12:59:53 Name Size Bytes Class Attributes E 1x1 3106 MException ans 1x3 24 double d 1x1 121 datetime datediff 1x1 128 duration mydatestr 1x6 12 char newdate 1x1 121 datetime newt 5x1 201 datetime newt1 1x1 147 datetime newt2 5x1 185 datetime olddate 1x1 121 datetime t 5x1 161 datetime t1 1x1 129 datetime tod 5x1 160 duration

So now we have decent working code, much nicer than the first successful attempt too!

P.S.

This also could have been done with a for-loop instead of working on the arrays as arrays. For a larger set of data, my colleague found that a for-loop solution (including working with the old-styledatestretc. tools) took 29.0 seconds, and the first vectorized version took 2.3 seconds. I'm guessing the second attempt is faster but didn't bother timing it.

你呢?

您是否一直在使用Release R2014B开始使用新的日期和时间功能?您成功或挣扎了什么?让我们知道这里.

In case it helps you, we also have a简短视频关于datetimethat you might be interested in. Happy computing!




Published with MATLAB® R2016a

|