parsing a time stamp without a colon

15 visualizzazioni (ultimi 30 giorni)
Fox
Fox il 21 Gen 2014
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 Commento
Walter Roberson
Walter Roberson il 21 Gen 2014
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 ?

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 21 Gen 2014
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 il 2 Feb 2019
Modificato: Siddharth Jose il 2 Feb 2019
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 Commenti
Walter Roberson
Walter Roberson il 2 Feb 2019
timestring = num2str(time, '%04d');
would not have the range problems and would not require any replacement of ' ' with '0'

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by