gpt4 book ai didi

string - 在 Matlab 中处理不同格式的日期字符串

转载 作者:行者123 更新时间:2023-12-04 06:34:00 25 4
gpt4 key购买 nike

我在 Matlab 中有一个很长的日期字符串向量(1000000+),我想将其转换为序列号格式。问题是并非每个日期字符串的格式都相同。它们是两种格式之一,

'2010-03-04 12:00:00.1'

或者

'2010-03-04 12:00:00'

问题是并非所有字符串都具有毫秒精度。没有关于这些没有毫秒的字符串在哪里出现的规则模式。数据最初是从数据文件中读取的,字符串当前以元胞数组的形式存在。我的工作如下:

for i=1:length(dates),
if length(dates{i})==19
dates(i)=datenum(temp);
elseif length(dates{i})==21
dates(i)=datenum(temp,'yyyy-mm-dd HH:MM:SS.FFF');
end
end

也许有更好的方法来解决这个问题?当它存在时,我保留毫秒精度是很重要的。这样做的目的是我必须根据不同的时间标准提取和计算与每次相关的数据的统计数据,我认为如果将日期作为数字处理会更容易。

最佳答案

在 MATLAB R2010b 中,我可以在调用 DATENUM 时获得所需的输出没有额外的格式参数:

>> dateStrs = {'2010-03-04 12:00:00.1'; ...  %# Sample strings
'2010-03-04 12:00:00'};
>> datenum(dateStrs)

ans =

1.0e+005 *

7.3420 %# The same? No, the Command Window just isn't displaying
7.3420 %# many places after the decimal point.

>> format long %# Let's make it show more decimal places
>> datenum(dateStrs)

ans =

1.0e+005 *

7.342015000011574 %# And there's the difference!
7.342015000000000

关于string - 在 Matlab 中处理不同格式的日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5046932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com