15 minutes intervals
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi everyone!
I have some mat files that look kind of like this:
19.10.09 09:00:16 1BASF.DE 39.45 39.43 39.41
19.10.09 09:00:16 1BASF.DE 39.45 39.44 39.43
19.10.09 09:00:20 1BASF.DE 39.46 39.45 39.43
… … … … … …
19.10.09 09:14:57 1BASF.DE 39.72 39.71 39.7
19.10.09 09:14:58 1BASF.DE 39.72 39.71 39.7
19.10.09 09:15:02 1BASF.DE 39.72 39.71 39.7
19.10.09 09:15:02 1BASF.DE 39.72 39.71 39.7
so the first colum is the date, the second column is the time stamp, the third is the name of the stock and the rest are prices. I want to create 15-minutes intervals that go from 9:00-9:15 am, then 9:15-9:30 am and so on until the end of the day and for these intervals the average price of the stock is taken. Do you have any ideas of how to make this?
I have seen people using datenum and the calculating the intervals , but the problem here is that my date and time stamps are in different columns and I dont know how to do it then.
Also, the timestamp column is a cell. Do you have any ideas that could help me out?
I would really appreciate it :)
Have a nice day!
댓글 수: 2
Laura Proctor
2011년 4월 18일
Could you elaborate on the data? How are you bringing it into MATLAB? A MAT-file implies that you have variables that you're importing into MATLAB. If that's the case, can you describe the relevant variables (such as their type and size)?
채택된 답변
Walter Roberson
2011년 4월 19일
Determine the first date of the range. Find the datenum() of midnight on that day. Convert the other values in to datenum() and subtract that base of midnight. Divide the resulting difference by (15*60)/(24*60*60) = 1/(24*60/15) = 1/(24*4) = 1/96 -- so multiply by 96 to do the division. Take the floor() of the result and that vector will be the number of 15 minute bins since midnight on the base date.
Once you have those bin numbers, you can add 1 to each of them and use the result as the subscript for the purposes of accumarray:
accumarray(1+binnum(:), stockprice(:), [], @mean)
댓글 수: 7
Walter Roberson
2011년 4월 20일
Okay, now class(date{1}{1})
If that shows char and length(date{1}) shows 1, then you have your strings wrapped in one too many layers. I think in that case
vertcat(date{:})
should be the cell array of strings ready to pass in to datenum.
추가 답변 (1개)
Laura Proctor
2011년 4월 19일
I imagine that the date and timestamps look something like the following varibles.
date = { '19.10.09'
'19.10.09'
'19.10.09'
'19.10.09' }
timestamp = { '09:00:16'
'09:00:17'
'09:00:18'
'09:00:19' };
You can convert these into a numeric date format. The variable t below contains the date and time together so that each row has the date and time in one number.
dateN = datenum(date,'dd.mm.yy');
timeF = datenum(timestamp,'HH:MM:SS') - datenum('00:00:00','HH:MM:SS');
t = dateN+timeF;
Here I have calculated the fraction that corresponds to 15 minutes, and then the ngroups variable tells how many groups will be contained in the data. This should hopefully get you started.
t15 = datenum('00:15','HH:MM') - datenum('00:00','HH:MM');
ngroups = ceil((max(t)-min(t))/t15)
댓글 수: 3
Walter Roberson
2011년 4월 19일
char(date) should convert it to a char array. However, that should not be needed: if you look at the bottom of datenum()'s documentation, http://www.mathworks.com/help/techdoc/ref/datenum.html
Calling datenum with more than one date string input returns a character array of converted date strings. Pass the multiple date strings in a cell array. All input date strings must use the same format. For example, the following command passes three dates that all use the mm/dd/yyyy format:
datenum({'09/16/2007';'05/14/1996';'11/29/2010'})
ans =
733301
729159
734471
Perhaps you are using a version of MATLAB too old to implement that?
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!