How to extract time using R from MATLAB serial date number?

I have a MATLAB serial number that I need to use in R, but I need to convert them to a normal date.

    Matlab:
   datestr(733038.6)
    ans =
    27-Dec-2006 14:24:00 

      

you can see that it gives the date and time.

Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01') 
> Matlab2Rdate(733038.6)
[1] "2006-12-27"

      

It only gives the date, but I need the time as well? Any idea

+3


source to share


1 answer


The Matlab trick is using "Jan 01, 0000", a fictitious reference date, to calculate its date number. The origin of the time for the "POSIXct" class in R is: "1970-01-01 00: 00.00 UTC. You can read about how different systems handle dates here .

Before converting, you need to account for this difference in the link from one format to another. This example contains the POSIX manual . Here's my output:



> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"

      

Where 719529 is 1970-01-01 00: 00.00 UTC in the Matlab tree structure and 86400 is the number of seconds in UTC standard time.

+6


source







All Articles