Day and Time in FreeMat

311 views
Skip to first unread message

Guilherme Lins

unread,
Sep 15, 2015, 7:33:46 AM9/15/15
to freemat
Hello everyone,

I am having some trouble in how to work with days and time inside FreeMat. I want FreeMat to read a time data from a *.csv file such as '03.09.2015 14:32:15' but also read the next columns which are regular numbers. It is also possible to convert this time data to a number in Excel and than read it as a regular number, but I would need to convert again this number inside FreeMat using some code such as "datestr" from MatLab, but again I didn't find an equivalent in FreeMat. Does anyone know how to handle this?

Thank you anyway,

Guilherme Lins.
 

Jonathan Weaver

unread,
Sep 16, 2015, 4:27:43 PM9/16/15
to freemat
You can convert it to a number in excel.  The number will be the number of days since 12/31/1899.  So 1 will be 1/1/1900, etc.  The time of my post is 9/16/2015 3:07 PM.  In Excel, as a number, this is 42263.63 .  I convert it to a number like say 20150916.1507 to represent the date in a more readable format.  There is no built in function to do that, so I wrote one.  I've pasted the code below (it lost the indenting when I pasted it).  I'm sure there is a better way or better code out there somewhere.

-Jonathan

function ddt = convertExcelDateTime(dt)

% Find the year

yr = 1900

leapyr = 0

if(dt > 31+29) % 1990 was not a leap year, though Excel thinks it is.

dt = dt - 1

end

while dt > 0

if(mod(yr,4) == 0)

if(mod(yr,100) == 0)

if(mod(yr,400) == 0)

dt = dt - 366

leapyr = 1

else

dt = dt - 365

leapyr = 0

end

else

dt = dt - 366

leapyr = 1

end

else

dt = dt - 365

leapyr = 0

end

yr = yr + 1

end

yr = yr - 1

dt = dt + 365

% Find the month

mnd = [31 28 31 30 31 30 31 31 30 31 30 31];

if(leapyr == 1)

mnd(2) = 29;

dt = dt + 1

end

mn = 1

while dt > 0

dt = dt - mnd(mn)

mn = mn + 1

end

mn = mn - 1

dt = dt + mnd(mn)

% Find the day, hour, minute, and second

dy = floor(dt)

tm = (dt - dy)*24

hr = floor(tm)

tm = (tm - hr)*60

min = floor(tm)

sec = floor((tm - min)*60)

ddt = yr*100.^2 + mn*100 + dy + hr/100+ min/100.^2 + sec/100.^3;

end

Reply all
Reply to author
Forward
0 new messages