parsing a time stamp without a colon

15 ビュー (過去 30 日間)
Fox
Fox 2014 年 1 月 21 日
コメント済み: Walter Roberson 2019 年 2 月 2 日
Hi everyone, I'm having trouble with a text parsing operation
I have a large vector of timestamps which are listed in military time, but without colons, like this
1520 1525 1530 etc.
but also
0 05 10
and
130 135 etc.
I need to put in :'s appropriately so that I can get this read to serial date time. How to do this? I am guessing it's a regexp thing, but I can't figure out the right syntax. Or is there an option under datestr / datenum / datevec?
I tried datestr with the times as they are (and the correct doy2date, which turned out well) and this just gives me back that every single time is at 0:00:00 which will not do, since this is sensitive streaming sensor data.
Thanks! Fox P. OSU
  1 件のコメント
Walter Roberson
Walter Roberson 2014 年 1 月 21 日
To check: you want 1520 to be 15:20? And you want 130 135 to be 01:30 01:35 ? What about 0 05 10? Are those to be 00:00 and 00:05 and 00:10 ?

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 1 月 21 日
Prefix each time with 3 '0's. Then take the last 4 characters. You can datenum('HHMM') the result without needing to put in a colon.
But do be careful: when you do not specify a year, the time will be interpreted relative to Jan 1 of the current year.
I would therefor suggest you use a different approach: str2double() the string. The minutes are the result mod 100, and the hours are floor(result / 100). You can then construct datevec structures of them with any appropriate values in the year month day slots, and datenum() a bunch of them at once. Or, of course, just do a direct calculation (hours * 60 + minutes) / (365 * 24 * 60 * 60). [Except in leap years.]

Siddharth Jose
Siddharth Jose 2019 年 2 月 2 日
編集済み: Siddharth Jose 2019 年 2 月 2 日
timestring=num2str(time); %This process will add blank spaces at zero posistion .eg:'930' will end up as '_930'
timestring(timestring == ' ') = '0'; %To replace the blank spaces with zeros.
%Here ends the answer to your question. optionally to create a proper datetime array follow the rest.
datestring=datestr(date);
dtcombined=datestring+""+timestring; %To avoid strcat limitations. Works on Matlab 2017b and later
datentime= datetime(dtcombined,'InputFormat','dd-MM-yyyyHHmm');
  5 件のコメント
Siddharth Jose
Siddharth Jose 2019 年 2 月 2 日
:D True.
Walter Roberson
Walter Roberson 2019 年 2 月 2 日
timestring = num2str(time, '%04d');
would not have the range problems and would not require any replacement of ' ' with '0'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by