gathering data corresponding to a string of dates

3 ビュー (過去 30 日間)
Richard
Richard 2012 年 3 月 9 日
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.

採用された回答

per isakson
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 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by