Datetime()返回错误的值

时间:2018-12-14 00:17:59

标签: matlab datetime

我正在尝试使用 datetime数组中x轴的值在matlab中进行绘制。
一个值的示例:“ 2016-06-03T13:37:20.315Z”
首先,将值保存到结构数组中,在这里我尝试将其复制到单独的数组中。我使用以下代码进行操作:

 timestamp=[];
 for j=1:length(data)
   timestamp = getfield(data,{j},'timestamp');
   timestamp{j}=(datetime);
end  

但是当我看一下数组时,似乎所有值都是一个日期,甚至不在“数据”结构数组中。
示例:

timestamp{1} = '14-Dec-2018 00:31:05';  
timestamp{10} = '14-Dec-2018 00:31:05';   
timestamp{19} = '14-Dec-2018 00:31:05'; 

我最初的想法是可能是因为输入格式,所以我尝试了

timestamp{j}=(datetime(timestamp,'InputFormat','uuuu-MM-dd''T''HH:mmXXX','TimeZone','UTC'));  

但是我收到一条消息:“ 使用日期时间出错(​​第635行) 无法使用格式'uuuu-MM-dd'T'HH:mmXXX'将'2016-06-03T13:37:20.315Z'转换为日期时间。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的代码中有许多错误。总结如下。

  1. 您正在循环获取存储在给定结构的字段中的数据。只需使用getfield()函数就可以直接获取结构中特定字段中存储的所有数据。
  2. 在循环中,您只是使用 datetime 函数只是填充timestamp变量。结果,每次调用datetime时,它都会返回当前时间,因此在每个位置使用相同的值填充数组。
  3. 您不能直接将具有TimeZone偏移量的日期char数组直接转换为datetime格式。您需要先使用strrep()命令删除偏移量。

问题解决方案

  
      
  1. 您应该使用getfield函数从以下位置访问时间戳char数组   不用循环就可以将数据结构完全放在一行中。
  2.   

-

  
      
  1. 您应该首先从日期char数组中删除偏移量字段(T和Z),并且
  2.   

-

  
      
  1. 最后,应使用要在绘图中使用的格式将编辑的日期时间字符串转换回 datetime 类型。
  2.   

下面给出了说明该过程的代码。

% suppose we have a struct named data 
% with a field known as timestamp holding the
% different datetime char arrays of particular 
% TimeZone offsets
data.timestamp = {'2016-06-03T13:37:20.315Z', '2016-07-10T17:45:20.200Z', ...
                '2016-07-09T13:37:21.305Z', '2016-11-10T01:30:20.320Z'};

% now getting the timestamp field elements in a variable using getfield()
% command
timestamp = getfield(data, 'timestamp')

% now removing each offsets chars(T and Z) from each datetime strings
edited_timestamp = strrep(timestamp, 'T', ' '); % removing T
edited_timestamp = strrep(edited_timestamp, 'Z', '') % removing Z

% finally, convert the edited_timestamp back to datetime type using the
% required format to use in plot
timestamp_datetime = datetime(edited_timestamp, 'Format', 'yyyy-MM-dd HH:mm:ss.SSS')

% ------------------------------------------------------
% now you can do the plotting here using timestamp_datetime
% ------------------------------------------------------
% e.g., plot(timestamp_datetime, [1:4])

输出

enter image description here

enter image description here

答案 1 :(得分:0)

您的索引编制和类型转换有点混乱,请参阅我的评论...

% Your code:
timestamp=[];                                     
for i=1:length(data)                              % Your loop variable is "i"
    % You override the entire "timestamp" array here, with element "j" not "i" of "data"
    % You also don't need to use getfield here
    timestamp = getfield(data,{j},'timestamp');  
    % You override element "j" (again, not "i") with the CURRENT datetime.
    % This line doesn't do any type conversion, "datetime" with no arguments is now!
    % Also you're using curly braces for indexing, which isn't right for a datetime array
    timestamp{j}=(datetime);
end 

您可以按以下步骤纠正此问题:

timestamp = NaT(1,length(data)); % pre-allocate the output to "not a time" array
for ii = 1:length(data)
    t = getfield( data, {ii}, 'timestamp' );
    t( t == 'T' | t == 'Z' ) = []; % remove T and Z, they will break datetime
    timestamp( ii ) = datetime( t, 'InputFormat', 'yyyy-MM-ddHH:mm:ss.SSS' );
end

输出:

timestamp = 
 1×2 datetime array
 03-Jun-2016 13:37:20   03-Jun-2016 13:37:21

(使用示例字符串创建,并添加了相同的字符串并添加了一秒钟)。


这是纠正您的代码的方法,这是我的处理方法:

timestamp = regexprep( {data.timestamp}, '[TZ]', '' );
timestamp = datetime( timestamp, 'InputFormat', 'yyyy-MM-ddHH:mm:ss.SSS' );   
相关问题