MATLAB Answers


Create time intervals of a time vector

Asked by Lu
on 14 May 2011
Hi everyone!
I´m trying to create 15-minute intervals of a time vector. My original time vector is as follows:
up to the closing hour of the exchange
Is there anyway I could reduce this time vector to just something like :
up until the closing of the exchange
I would really appreciate any help, because I have been trying first to convert the time vector into numeric data and from that calculating the 15-minute intervals, but from there I don´t know how to bring it back to a time vector format. Especially because I am also computing 15-minute intervals of stock prices and I need to have it all together in a matrix of file together like this :
All=[Date Time Stock Price]
Once again thank you very much and have a very nice weekend :)


Sign in to comment.

1 Answer

Answer by Oleg Komarov on 14 May 2011
 Accepted Answer

Vector of 9:00 - 17:30 with 15 mins increments (24 hours a day, 96 quarters a day)
dv = 9/24:1/96:17/24+2/96;
Visualize result
ans =
9:00 AM
9:15 AM
5:30 PM
If you want to apply the solution to a range of days (eventually excluding saturdays and sundays):
1. Initial and final date
infi = {'15/01/2011'
days = infi(1):infi(2);
infi = datenum(infi,'dd/mm/yyyy');
2. Exclude sundays and saturdays (uncomment)
% days = days(~ismember(weekday(days),[1 7]));
3. Add dv
out = bsxfun(@plus, days, dv.');
4. Visualize result


Show 1 older comment
You cannot concatenate strings with double in a matrix (or more generally in a double array).
You should use a cell array:
{'01/01/2001' '09:30 AM' 23.23 23.29
'01/01/2001' '09:45 AM' 23.15 0.01} % Oh noooooooooo!!!! :)
If you want to save it as a txt, then go ahead and use the cell array.
Otherwise, I would suggest to keep the numeric dates:
- less memory consumption
- faster
- easier to manipulate
If you're gonna use several days for several securities you're not gonna scroll visually the dates anyways!
Dear Oleg,
Thank you so much for your help! I still don´t know what to do, because as you say, I have several days for several securities, and I need kind of an efficient way of visualizing the data set as
Day Hour Stock Price1 Price 2
and I guess saving my output as a .mat files seems a bit easier for me because I have so much trouble loading .txt files using textscan :S. Is there a way of going back from numeric dates to normal dates?
Have a nice weekend!! :)
You can plot the series with the numeric dates and then use datetick to adapt to convert the labels to the desired date format (more feasible than scroolling down a cell array)
In general, to convert the numeric date to stringdate use datestr.
If you have troubles with textscan, open a new thread, post the first lines of your file, the code you're using (formatted), any error and the request for what you're trying to achieve.

Sign in to comment.