parsing a time stamp without a colon
Afficher commentaires plus anciens
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 commentaire
Walter Roberson
le 21 Jan 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 ?
Réponses (2)
Walter Roberson
le 21 Jan 2014
0 votes
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
le 2 Fév 2019
Modifié(e) : Siddharth Jose
le 2 Fév 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 commentaires
Walter Roberson
le 2 Fév 2019
The loop
for i = 1:length(timestring) %To replace the spaces with zeros.
if timestring(i)==' '
timestring(i) = '0';
end
end
can be replaced with no loop with
timestring(timestring == ' ') = '0';
Note that the code will fail if it happens that all of the entries are less than 1000 . Leading spaces will only be put in place by num2str() to make all of the entries the same length, not any particular length.
Siddharth Jose
le 2 Fév 2019
Modifié(e) : Siddharth Jose
le 2 Fév 2019
Well, the only reason to have added the following code segment
for i = 1:length(timestring) %To replace the spaces with zeros.
if timestring(i)==' '
timestring(i) = '0';
end
end
is to add zero's in the blank spaces that occur before 1000 as matlab converts '930' to '_930'. '_'-symbolic for space. And you'll have to modify it a bit for times between midnight and 00:59.
In all other cases it works.
Thank you for mentioning
"timestring(timestring == ' ') = '0'; "
does the job better.I've edited my answer too.:)
Walter Roberson
le 2 Fév 2019
You would need to modify for times between 00:00 and 09:59 -- unless, that is, there happened to be at least one entry from 10:00 onwards in the mix.
Siddharth Jose
le 2 Fév 2019
:D True.
Walter Roberson
le 2 Fév 2019
timestring = num2str(time, '%04d');
would not have the range problems and would not require any replacement of ' ' with '0'
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!