Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

matlab

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

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

you can see 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 gives only the date but I need also the time? Any idea

like image 405
sacvf Avatar asked May 06 '15 08:05

sacvf


1 Answers

The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of 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 reference from one format to another. The POSIX manual contains such an example. 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 Matlab's datenum and 86400 the number of seconds in an standard UTC day.

like image 186
brodoll Avatar answered Sep 28 '22 05:09

brodoll