How to extract data corresponding to a date and time?

64 vues (au cours des 30 derniers jours)
maruljay
maruljay le 15 Jan 2020
Modifié(e) : Adam Danz le 16 Jan 2020
I have a time series data W (315360000x1)
The number of rows is equal to 6000 x 144 x 365 , which is 6000 measures every 10 minutes (sampling rate 10 Hz) times the number of 10 minutes in a day (144) times the number of days in a year (365). W is thus obtained for the year 2011.
I have another file T.csv, which contains just the dates and times. I have imported this file into matlab as a cell array and converted it onto a datetime file (t) as shown below:
I want to use the time in 't' and extract data 1 hour before the given time and 1 hour after the given time (i.e extract 2 hours of data)
For ex: For t(1,1) which is 2015/10/04 13:38:33, I want to get data from W corresponsing to (2015/10/04 12:28:33 to 14:28:33)
This is how far I've gotten:
t1 = datetime(2011,01,01,00,00,00);
t2 = datetime(2011,12,31,23,59,59);
dates = t1:seconds(0.1):t2;
tminus=t-minutes(60);
tplus=t+minutes(60);
Thanks,

Réponse acceptée

Adam Danz
Adam Danz le 15 Jan 2020
Modifié(e) : Adam Danz le 16 Jan 2020
Here's how to get the indices of the datetime array that are within +/- 1 hour of the target time.
% Create datetime array
t1 = datetime(2011,01,01,00,00,00);
t2 = datetime(2011,02,01,00,00,00);
dates = t1:seconds(0.1):t2;
% Determine which datetime values are within
% +/- 1 hour from time t
t = datetime(2011,01,15,12,30,00);
idx = dates > t-hours(1) & dates < t+hours(1);
idx is a logical vector the same size as dates.

Plus de réponses (1)

Corey Silva
Corey Silva le 16 Jan 2020
My understanding is you want to associate your data in a cell array with a datetime array so that you can index into the data by time.
If I'm correct, I think you would have a much better time first reading your data from the first file into a table via the readtable function. Then you could use table2timetable with your table and the datetime array to make a timetable from the data and timestamps. If you had both the timetsamps and the time series data in a single file, you could simply use readtimetable to get this data neatly into a timetable in matlab.
If your data is already in matlab, you can skip the above steps and simply use the the timetable constructor.
With a timetable in MATLAB you should have no problem indexing into your data via timerange.
% Import the data into a table.
t = readtable('timeseriesdata.csv');
% The next line will give you a timetable with your datetime array as your rowtimes.
tt = table2timetable(t,'RowTimes',dates);
Note that the above code will not work for the way you defined dates as of right now because it is size 1x315359991 and your data is 315360000x1. You need to have one time stamp per row of data in a timetable. Since it seems you know the start time and sample rate of your timestamps, you could simply define these in the table2timetable call.
tt = table2timetable(t,"StartTime",startdate,'SampleRate',sr);
Now, with the data in a timetable you can use timerange
S = timerange(starttime,endtime,'hours');
extractedTT = TT(S,:);

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by