Convert Matlab Data to Datetime

-I want to convert Matlab serial time (amount of data, as in t_matlab=now)
to C # Datetime (as in var t_cs = DateTime.Now.Ticks).

Any idea how to do this?

[Edit] I found a way to do this, but still not sure if this is the best way.
[Edit2] DateTimes bug fixed, thanks Jonas!

function cstime = datenum2datetime( matlabSerialTime )
%Convert matlab serial time (datenum) to .net datenum.
%
%   Example:
%   ntTime = datenum2datetime(now)
%   cstime = datenum2datetime([734539.4717013890 734539.5051388888]);
%
%   See also datenum.

%   using System.DateTime.Parse(string).Ticks to convert to DateTime format.
%   t1 = now; t2 = now+1; matlab_times = [t1 t2];
%   cs_times = [System.DateTime.Parse(datestr(t1)).Ticks ...
%            System.DateTime.Parse(datestr(t2)).Ticks]
%   aver = diff(cs_times)/diff(matlab_times);
%   offver = cs_times(1) - matlab_times(1)*aver;

a = 10^7*60*60*24;
offset = -367*10^7*60*60*24;
cstime = a*matlabSerialTime + offset;
+5
source share
2 answers

Edit : Surprised by jarr's answer, I explored further. It turns out that the approximate points in time that sharhar_m indicates in the question are incorrect (sorry for not checking before).

, , 367-281 = 86 ​​

function cstime = datenum2datetime( matlabSerialTime )
cstime = 10^7*60*60*24*(matlabSerialTime - 367);

, , - :

% {05-Feb-2011 11:19:15} System.DateTime - 634399319550000000

MATLAB R2010b

sdt =System.DateTime(634399319550000000); 
[sdt.Day sdt.Month]

[2 5], DateTime 2 , 5 ! ,

cs_times = [System.DateTime.Parse('05-Feb-2011 11:19:15').Ticks ...
            System.DateTime.Parse('05-Feb-2011 12:07:24').Ticks]

[634325015550000000 634325044440000000].

a 10 ^ 7 * 60 * 60 * 24, .. MATLAB / "" ( ), # "", 10 ^ -7 ". , , a.

b, (b/a), , " " 367 ; b 281 . MATLAB datestr

" 1 0000 . 0000 ".

( datestr(0,'dd-mm-yyyy HH-MM-SS') , 0--0000). # ticks -

" 100- 12:00:00 , 1 0001 , DateTime.MinValue. , ".

, " " , , 367 . , , 8- ... , ; -).

+5

, Matlab System.DateTime:

function datetimeticks = mt2dt(matlabserialtime) 
    datetimeticks = (matlabserialtime - 367)*86400/1e-7;  
end

( Matlab):

mdt = datenum('8/6/1901 07:50:13');  
sdt = System.DateTime(mt2dt(mdt));

>> sdt.ToString

ans = 

8/6/1901 07:50:13

System.DateTime Matlab :

function matlabserialtime = dt2mt(datetimeticks)
    matlabserialtime = double(datetimeticks) * 1e-7/86400 + 367;
end

( Matlab):

sdt = System.DateTime.Parse('8/6/1901 07:50:13');
mdt = datenum(dt2mt(sdt.Ticks));

>> datestr(mdt)

ans =

06-Aug-1901 07:50:13

, DateTimeKind, Matlab DateTime.
, , SpecifyKind DateTime ctor.

+2

All Articles