Convert Timestamp into Date Vector

6 ビュー (過去 30 日間)
Joshua
Joshua 2017 年 12 月 19 日
編集済み: Walter Roberson 2017 年 12 月 19 日
I have a timestamp like "x2017_12_1816:00_00". It is in a format "xyyyy_mm_dd_hh:mm:ss". However, matlab will not allow this to convert to a date vector since it's not in an acceptable format. I've been able to convert it to a date format by using the code below, where structFields is a structure containing the unconverted timestamps. As you might imagine, this method takes a long time. I've tried to accomplish the same effect by first converting the timestamps to a number in the format "yyyymmddhhmmss", but have yet to come up with a more efficient algorithm. Any suggestions?
if true
% code
structEdit1 = cellfun(@(x) replaceBetween(x,13,15,':','Boundaries','exclusive'),structFields,'UniformOutput',false);
structEdit2 = cellfun(@(x) replaceBetween(x,16,18,':','Boundaries','exclusive'),structEdit1,'UniformOutput',false);
structEdit3 = cellfun(@(x) strrep(x(),'_','-'),structEdit2,'UniformOutput',false);
structEdit4 = cellfun(@(x) insertAfter(x,11,' '),structEdit3,'UniformOutput',false);
structEdit5 = cellfun(@(x) erase(x,'x'),structEdit4,'UniformOutput',false);
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 19 日
編集済み: Walter Roberson 2017 年 12 月 19 日
datetime('x2017_12_1816:00_00', 'InputFormat', '''x''yyyy_MM_ddHH:mm_ss', 'Format', 'yyyyMMddHHmmss')
The tricks here:
  • datetime uses MM for month and mm for minutes
  • datetime uses HH for 24 hour hour
  • any alphabetic character that is intended to be used literally must be put inside '' inside the string.
>> disp('''x''yyyy_MM_ddHH:mm_ss')
'x'yyyy_MM_ddHH:mm_ss

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by