how to solve date serial number contains gap?

4 views (last 30 days)
Noor
Noor on 25 Aug 2017
Commented: Noor on 29 Aug 2017
I have more than 100000 dates and time data in one column [VariableA].I already convert it to serial date numbers by using datenum([VariableA]).However, there is the gap between the date. Its skip one month for example 1 January 1975,1 February 1975,1 March 1975.... It seems Matlab read as mm/dd/yy..but when I change my data as this format, there is an error. Please help me to solve this problem. Thank you for your help.
  3 Comments
Noor
Noor on 29 Aug 2017
what I mean is when I convert it, suppose 1/1/75,2/1/75,3/1/75 in serial date number format... but the answer is 1/1/75,1/2/75,1/3/75 .. it looks like datenum read as mm/dd/yy..how i want to convert date and time to date serial number? i already combine column date and time together, however, the answer has the missing value. When I try to convert date alone by datenum,it works. but not when I combine date and time. please help me..

Sign in to comment.

Answers (1)

Rik
Rik on 29 Aug 2017
If you have trouble with a function, you should have a look at the documentation (type doc datenum or Google it). If you would have taken a look at it, you might have noticed you can put in a formatIn variable. The formatting of this string can be a bit tricky, so look very carefully if you have followed EXACTLY what the list is showing (and post an update here if you still have trouble getting it right).
%assuming a format like '1 February 1975 10:50 AM'
DateString='1 February 1975 10:50 AM';
formatIn = 'dd mmmm yyyy HH:MM PM';
serial_date=datenum(DateString,formatIn);
%see it is correct:
disp(datetime(serial_date,'ConvertFrom','datenum'))
It is also a good idea to answer the question KL posted in his comment, as the documentation notes: "The best way to represent points in time is by using the datetime data type. The best way to represent elapsed time is by using the duration or calendarDuration data types." These data types may not be available on your release (they were introduced in R2014b).
For future reference: have a read here and here. It will greatly improve your chances of getting an answer.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!