gathering data corresponding to a string of dates

1 visualización (últimos 30 días)
Richard
Richard el 9 de Mzo. de 2012
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.

Respuesta aceptada

per isakson
per isakson el 9 de Mzo. de 2012
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 ] )

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by