parsing a time stamp without a colon
7 次查看(过去 30 天)
显示 更早的评论
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
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
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.]
0 个评论
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 个评论
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 Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!