Generating array of consecutive timestamps in double

2 Ansichten (letzte 30 Tage)
Ephedyn
Ephedyn am 11 Sep. 2012
Kommentiert: Peter Perkins am 31 Aug. 2017
I need to generate an array of consecutive timestamps, in double and yyyymmddHHMMSS format, for specified start and end times. For example:
timestamps(1) = 2.012082104000000e+013;
timestamps(2) = 2.012082104000100e+013;
...
...
timestamps(120) = 2.012082104020000e+013;
I wrote a working piece of code, but the str2double/datestr combination in the parfor loop is too slow (I used 120 seconds as an example, but in actual use, I will be generating about 10^7 consecutive seconds of timestamps). Is there a faster way to do this?
if matlabpool('size') == 0
matlabpool local
end
startDate = num2str(2.012082104000000e+013);
endDate = num2str(2.012082104020000e+013);
startDateyyyy = str2double(startDate(:,1:4));
startDatemm = str2double(startDate(:,5:6));
startDatedd = str2double(startDate(:,7:8));
startDateHH = str2double(startDate(:,9:10));
startDateMM = str2double(startDate(:,11:12));
startDateSS = str2double(startDate(:,13:14));
startDateVec = [startDateyyyy startDatemm startDatedd startDateHH ...
startDateMM startDateSS];
startDate = datenum(startDateVec);
endDateyyyy = str2double(endDate(:,1:4));
endDatemm = str2double(endDate(:,5:6));
endDatedd = str2double(endDate(:,7:8));
endDateHH = str2double(endDate(:,9:10));
endDateMM = str2double(endDate(:,11:12));
endDateSS = str2double(endDate(:,13:14));
endDateVec = [endDateyyyy endDatemm endDatedd endDateHH ...
endDateMM endDateSS];
endDate = datenum(endDateVec);
timestamps = startDate:(1/24*1/60*1/60):endDate;
parfor i = 1:length(timestamps)
timestamps(i) = str2double(...
datestr(timestamps(i),'yyyymmddHHMMSS'));
end
Thanks in advance!
  3 Kommentare
Ephedyn
Ephedyn am 11 Sep. 2012
Bearbeitet: Ephedyn am 11 Sep. 2012
^Thanks for your prompt response! It has to do with two reasons, mainly for standardization and what looks more pleasing to the eye. The function that I'm writing imports time series data fetched from a brokerage API, and this is the standard format for their timestamps and the rest of the programs I've written to manipulate the data.
Thereafter, the function runs a loop of tests to catch missing data points from the data feed and cleverly fill them in so there are no discontinuities - and other things, so the timestamps I've generated must match the format of the timestamps from the data feed to carry out these tests. I've written it all to work in this format before going back to profile my code, and figured it would be easier to optimize this part alone than to change everything. (: Any suggestion?
Oleg Komarov
Oleg Komarov am 11 Sep. 2012
As you can see from my answer, the appearance of dates can be controlled with datestr() and I still recommend to store dates as MATLAB serial numbers produced with datenum(). I am posting an alternative solution to let you see how bothersome is to store dates in a different way.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Oleg Komarov
Oleg Komarov am 11 Sep. 2012
Bearbeitet: Oleg Komarov am 11 Sep. 2012
I recommend to use MATLAB serial dates as produced by datenum(). Any manipulation with dates becomes much easier. The appearance can be controlled with datestr().
% Generate serial dates increasing by one second from 2012-08-21 04:00:00
dates = datenum(2012,08,21,4,0,0:120)
% Visualize them in format 31 yyyy-mm-dd HH:MM:SS
datestr(dates,31)
Alternatively:
st = 20120821040000;
en = 20120821040200;
sten = [st; en];
y = floor(sten/1e10);
m = floor(rem(sten, 1e10)/1e8);
d = floor(rem(sten, 1e8)/1e6);
HH = floor(rem(sten, 1e6)/1e4);
MM = floor(rem(sten, 1e4)/1e2);
SS = rem(sten, 1e2);
% Again MATLAB serial dates
stenser = datenum(y,m,d,HH,MM,SS);
dates2 = stenser(1):1/(60^2*24):stenser(2);
% Controlling appearance
datestr(dates2,31);
% Now you have to reverse into yyyymmddHHMMSS
dtvc = datevec(dates2);
out = dtvc(:,1)*1e10 + dtvc(:,2)*1e8 + dtvc(:,3)*1e6 +...
dtvc(:,4)*1e4 + dtvc(:,5)*1e2 + dtvc(:,6);
  1 Kommentar
Ephedyn
Ephedyn am 11 Sep. 2012
Exactly what I need for now! I will take your advice and try to clean up everything to MATLAB serial dates as produced datenum. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Amarjit Dhillon
Amarjit Dhillon am 31 Aug. 2017
Let's say we want to generate timestamps in the format of yyyymmddHHMMSS for 10 days
1-Jan-2017 at 12:00:00 to 10-Jan-2017 till 12:00:00, This can be easily generated by below-written code
t1 = datetime(2017,1,1,12,0,0);
t2 = datetime(2017,1,10,12,0,0);
data = t1:seconds(1):t2;
datestr(data,'yyyymmddHHMMSS')
  1 Kommentar
Peter Perkins
Peter Perkins am 31 Aug. 2017
Amarjit, your suggestion to use datetime is a good one (the OP was from 1012, before datetime existed in MATLAB). But actually, there's usually no reason to convert it to text. Just change the format:
>> data.Format = 'yyyymmddHHMMSS'
data =
1×3 datetime array
20170001120100 20170001120100 20170001120100 ...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Time Series Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by