MATLAB: How to parse a timestamp in the format yyyy/doy/hh/mm/ss

timetime stamp

I am running R2014b. How can I parse a UTC timestamp in the format yyyy/doy/hh/mm/ss where doy (day of year) is an integer value 1 to 366. I want to convert doy/hh/mm/ss into seconds.

Best Answer

One approach:
str = '2016/183/13/20/15';
fieldc = regexp(str, '/', 'split'); % Parse
fieldn = cellfun(@str2num, fieldc); % Convert To Double
dn = datenum([fieldn(1), 01, 01]) + fieldn(2); % Convert To [Year Month Day]
dv = datevec(dn) + [0 0 0 fieldn(3:end)]; % Add ‘hh’ ‘mm’ ‘ss’ To Vector
secs = etime(dv, [fieldn(1) 01 01 0 0 0]); % Use ‘etime’ To Convert To Seconds From Beginning Of Year
This could possibly be vectorised, but it might be easiest to create it as a function file and parse the dates in a loop. This is a one-off for each set of data (save the converted vector of ‘seconds’), so small inefficiencies could be tolerable. You will probably have to tweak it to work with your data, but that should be straightforward.