how to get serial date numbers?

hello.i have a structure like this: data = struct('Date', {cell(2208,1)}, 'Hour', {zeros(2208,1)}, 'DryBulb', {zeros(2208,1)}, 'DewPnt', {zeros(2208,1)}, 'SYSLoad', {zeros(2208,1)}, 'NumDate', {zeros(2208,1)});.. 'Date' has 92 days and 'Hour' has 24 hours,from 1 to 24.how can i express 'Date' and 'Hour' in serial date numbers and to present these serial date numbers in 'NumDate'?thank you

 Accepted Answer

per isakson
per isakson on 16 Sep 2012
Edited: per isakson on 20 Sep 2012
Hint:
sdn = bsxfun( @plus, [0:91], transpose([0:23]/24) );
datestr( sdn(1 ), 'yyyy-mm-dd HH:MM:SS' )
datestr( sdn(end), 'yyyy-mm-dd HH:MM:SS' )
data.NumDate = sdn(:);
.
--- Working code in response to comment ---
Try this
sd1 = datenum( ' 1 Jan 2004', 'dd mmm yyyy' );
sd2 = datenum( '31 Mar 2004', 'dd mmm yyyy' );
sdn = bsxfun( @plus, [sd1:1:sd2], transpose([0:23]/24) );
datestr( sdn( 1), 'yyyy-mm-dd HH:MM:SS' )
datestr( sdn(end), 'yyyy-mm-dd HH:MM:SS' )
This code avoids round-off errors in sdn(1,:)
>> sdn( 1, 1:5 )
ans =
731947 731948 731949 731950 731951
>> all( sdn(1,1:5) == [ 731947, 731948, 731949, 731950, 731951 ] )
ans =
1
Whether it is wise to rely on this for comparisons is questionable
And to make sdn a column vector
sdn = sdn(:);

7 Comments

datestr() does not produce serial date numbers
per isakson
per isakson on 16 Sep 2012
Edited: per isakson on 16 Sep 2012
That's right, but sdn are serial date numbers. I included datestr() as a check of what's in sdn. However, I missed the copy of the question and I guess I missed your message. "bsxfun" serves the purpose of "'ABC'-0".
per isakson,thank you very much for your help.this code which you gave to me produces serial date numbers only for 23 hours and not for 24.please tell me how can i fix it.
Try
length([0:23])
the hours are numbered [0,1,..,22,23].
Further, try
plot( sdn, '.' )
and zoom in on the midnight.
Hour 23 stands for the interval 23:00:00 to 23:59:59.
per isakson, i tried your code to convert date and time to serial date numbers but i have a problem. i try different dates but the serial date numbers dont change. they are always the same numbers. for example,my interval of dates is from 1 Jan 2004-31 March 2004. the answer always is 0000-01-00 00:00:00 until 0000-03-31 23:00:00. why doesnt take the year 2004 but takes 0000? please help me.thanks.
per isakson
per isakson on 19 Sep 2012
Edited: per isakson on 20 Sep 2012
A hint is a hint nothing more. I'm convinced one should not use code, which one doesn't understand. Seriously, there is nothing in my hint that could possibly account for your dates.
See my answer above
thank you very much per isakson..:)..i did a mistake with the first code but anyway, thank you again..:)

Sign in to comment.

More Answers (0)

Categories

Asked:

UPT
on 16 Sep 2012

Community Treasure Hunt

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

Start Hunting!