从 UTC 到日期向量

发布于 2024-12-11 18:07:56 字数 516 浏览 0 评论 0原文

我有一组以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨夜星沙 2024-12-18 18:07:56

正如 Marc 所建议的,您所要做的就是通过指定数据格式来“帮助”MATLAB。

datevec('2010-07-31T23:01:57Z','yyyy-mm-ddTHH:MM:SS')

应该可以解决问题。

As Marc suggests, all you have to do is 'help' MATLAB a bit by specifying the data format.

datevec('2010-07-31T23:01:57Z','yyyy-mm-ddTHH:MM:SS')

should do the trick.

夏夜暖风 2024-12-18 18:07:56

使用 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 the datevec() 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.

海拔太高太耀眼 2024-12-18 18:07:56

正如其他人所述,您可以将预期日期字符串的格式指定为 DATEVEC 等函数DATENUM。示例:

%# set of dates and an initial date
T0 = '2010-01-01T00:00:00Z';
T = repmat({'2010-07-31T23:01:57Z'}, [10 1]);   %# cell array

%# format of datetime
frmt = 'yyyy-mm-ddTHH:MM:SS';

%# convert to serial date numbers, and compute difference between all T and T0
n = datenum(T,frmt);
n0 = datenum(T0,frmt);
tdiff = bsxfun(@minus, n, n0);

或者,如果您有不寻常的日期格式,您可以随时使用 TEXTSCAN

vec = zeros(numel(T),6);
for i=1:numel(T)
    C = textscan(T{i}, '%f', 'Delimiter',['-' ':' 'T' 'Z']);
    vec(i,:) = C{1};
end
n = datenum(vec);

请注意,在这两种情况下,您都可以使用 DATESTR 函数。

As stated by others, you could specify the format of the expected date strings to functions like DATEVEC and DATENUM. Example:

%# set of dates and an initial date
T0 = '2010-01-01T00:00:00Z';
T = repmat({'2010-07-31T23:01:57Z'}, [10 1]);   %# cell array

%# format of datetime
frmt = 'yyyy-mm-ddTHH:MM:SS';

%# convert to serial date numbers, and compute difference between all T and T0
n = datenum(T,frmt);
n0 = datenum(T0,frmt);
tdiff = bsxfun(@minus, n, n0);

Alternatively, if you have an unusual date format, you can always split and parse it yourself with functions like TEXTSCAN:

vec = zeros(numel(T),6);
for i=1:numel(T)
    C = textscan(T{i}, '%f', 'Delimiter',['-' ':' 'T' 'Z']);
    vec(i,:) = C{1};
end
n = datenum(vec);

Note that in both cases, you can convert back serial times to date strings with the DATESTR function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文