gathering data corresponding to a string of dates
3 次查看(过去 30 天)
显示 更早的评论
I have a vector of temperatures and a cell array of Date/Time. I want to create 4 variables where each variable represents different stages of the day. For example, I need to create a variable for all of the temperatures recorded between 09:00 - 15:00, 15:00-21:00, 21:00-03:00, and 03:00-09:00 for the entire year.
So, if the temperature was recorded hourly for a year I would have 8760 measurements, I would like to break this down into separate variable corresponding to the times shown above.
clear all
StartDate = '2011-01-01 00:00';
EndDate = '2011-12-31 23:57';
Resolution = 60;
DateTime=datestr(datenum(StartDate,'yyyy-mm-dd HH:MM'):Resolution/(60*24):...
datenum(EndDate,'yyyy-mm-dd HH:MM'),'yyyy-mm-dd HH:MM');
DateTime=cellstr(DateTime);
data = 1 + (20-1).*rand(8760,1);
So, in the end I will have a variable which has the temperatures between those times taken each day for the entire year. Will this be possible with the current format of the DateTime vector? If I convert it to decimal days then I'm afraid I'll lose track of the times which I need. Thanks for your time.
0 个评论
采纳的回答
per isakson
2012-3-9
Logical indexing is the key I would say:
data = 1 + (20-1).*rand(8760,1);
time = datenum( StartDate ) + (0:8760-1)/24;
dvec = datevec( time );
data1 = data;
data2 = data;
data3 = data;
data4 = data;
data1( not( 3 <= dvec(:,4) & dvec(:,4) < 9 ) ) = nan;
data2( not( 9 <= dvec(:,4) & dvec(:,4) < 15 ) ) = nan;
data3( not( 15 <= dvec(:,4) & dvec(:,4) < 21 ) ) = nan;
data4( not( 21 <= dvec(:,4) | dvec(:,4) < 3 ) ) = nan;
figure, plot( time, [data1, data2, data3, data4 ] )
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!