parsing a time stamp without a colon
조회 수: 13 (최근 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!