从 UTC 到日期向量
我有一组以 2010-07-31T23:01:57Z
形式表示的日期。我需要将其转换为向量。我可以将其作为 datavec(mydate)
执行,它会自动将字符串转换为向量,但此函数不接受 UTC 字符串日期。所以我粗鲁地以这种方式解决:
a = `2010-07-31T23:01:57Z`; %UTC date format
a(20) = ''; %remove Z
a(11) = ' '; %replace T with a space
a = datavec(a); % [2010, 7, 31, 23, 1, 57]
这样 a
是一个日期向量,我可以使用 etime(T1, T0)
来计算 T1< 之间的时间差/code> 和
T0
。这是独特的方式还是我可以做一些时尚的事情?
I have a set of date expressed in form of 2010-07-31T23:01:57Z
. I need to transform it into vector. I could do this as datavec(mydate)
it will transform automatically the string into vector, but this function doesn't accept UTC string date. So I have rudely resolved in this way:
a = `2010-07-31T23:01:57Z`; %UTC date format
a(20) = ''; %remove Z
a(11) = ' '; %replace T with a space
a = datavec(a); % [2010, 7, 31, 23, 1, 57]
In this way a
is a datevector and I can use etime(T1, T0)
to compute time difference between T1
and T0
. Is this the unique way or I can do something stylish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Marc 所建议的,您所要做的就是通过指定数据格式来“帮助”MATLAB。
应该可以解决问题。
As Marc suggests, all you have to do is 'help' MATLAB a bit by specifying the data format.
should do the trick.
使用
datevec()
调用的fieldSpecIn
部分。在这里阅读所有相关内容:http://www.mathworks.com/help/techdoc /matlab_prog/bspgcx2-1.html#bspgc4m-1
除了字符串末尾的 TZ 规范以及 - 和:'s,您的格式与标准 ISO 8601 格式相同。您可以修改自己的规范字符串,也可以创建自己的规范字符串。
请注意,Matlab 时间没有时区的概念。您必须自己跟踪这一点。
use the
fieldSpecIn
part of thedatevec()
call. Read all about it here:http://www.mathworks.com/help/techdoc/matlab_prog/bspgcx2-1.html#bspgc4m-1
Except for the TZ spec at the end of your string and the -'s and :'s, yours is identical to the standard ISO 8601 format. You can either modify yours, or create your own spec string.
Please note that Matlab time has no concept of time zone. You must keep track of that yourself.
正如其他人所述,您可以将预期日期字符串的格式指定为 DATEVEC 等函数 和 DATENUM。示例:
或者,如果您有不寻常的日期格式,您可以随时使用 TEXTSCAN:
请注意,在这两种情况下,您都可以使用 DATESTR 函数。
As stated by others, you could specify the format of the expected date strings to functions like DATEVEC and DATENUM. Example:
Alternatively, if you have an unusual date format, you can always split and parse it yourself with functions like TEXTSCAN:
Note that in both cases, you can convert back serial times to date strings with the DATESTR function.