Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Matlab中不同格式日期字符串的处理_String_Matlab_Date - Fatal编程技术网

String Matlab中不同格式日期字符串的处理

String Matlab中不同格式日期字符串的处理,string,matlab,date,String,Matlab,Date,我在Matlab中有一个很长的日期字符串向量(1000000+),我想将其转换为序列号格式。问题在于并非每个日期字符串的格式都相同。它们是两种格式之一 “2010-03-04 12:00:00.1” 或 “2010-03-04 12:00:00” 问题是并非所有字符串都具有毫秒精度。对于这些不带毫秒的字符串出现的位置,没有规则模式。数据最初是从数据文件读取的,字符串当前作为单元格数组存在。我的工作如下: for i=1:length(dates), if length(dates{i})

我在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中,我能够在调用时获得所需的输出,而无需额外的格式参数:

>> 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