How to reverse convert InputFormat of datetime constructor in Matlab? - matlab

How to reverse convert InputFormat of datetime constructor in Matlab?
datestr doesn't work:
>> startTime = datetime('2014/06/01-00:00', 'InputFormat', 'yyyy/MM/dd-HH:mm')
startTime =
datetime
01-Jun-2014 00:00:00
>> datestr(startTime, 'yyyy/MM/dd-HH:mm')
ans =
2014/00/01-00:06
As you see, it turns months into minutes.
How to overcome?
UPDATE
Format should not be hardcoded, solution should work in both ways with one given format string.

Set the 'Format' of your datetime object to the desired output and use char():
tfmt = 'yyyy/MM/dd-HH:mm';
startTime = datetime('2014/06/01-00:00', 'InputFormat', tfmt, 'Format', tfmt);
char(startTime)
Which returns:
>> SOcode
ans =
'2014/06/01-00:00'
Note that this is a documented limitation of datestr
Note
The symbolic identifiers describing date and time formats are different from those that describe the display formats of datetime arrays.

Rearranging the out format (very small change) of datestr worked:
startTime = datetime('2014/06/01-00:00', 'InputFormat', 'yyyy/MM/dd-HH:mm')
startTime =
01-Jun-2014 00:00:00
datestr(startTime, 'yyyy/mm/dd-HH:MM')
ans =
2014/06/01-00:00

Related

Why isn't the year in my categorical array time properly converted in Matlab?

I have the following catergorical array in Matlab
time(1:3)
ans =
3×1 categorical array
13-10-19 00:03
13-10-19 00:08
13-10-19 00:12
I want to use it as x-axis for a timeseries, in order to do so I need it as time string, so what I have done is the following
string(time(1:3))
ans =
3×1 string array
"13-10-19 00:03"
"13-10-19 00:08"
"13-10-19 00:12"
If I cavert it using either datetime or datestr the year seems to be lost or wrongly converted
datetime(string(time(1:3)))
ans =
3×1 datetime array
19-Oct-0013 00:03:00
19-Oct-0013 00:08:00
19-Oct-0013 00:12:00
datestr(string(time(1:3)))
ans =
3×20 char array
'19-Oct-0013 00:03:00'
'19-Oct-0013 00:08:00'
'19-Oct-0013 00:12:00'
Why is this? How can I convert the year properly?
Thanks in advance!
you have to specify the InputFormat:
datetime(string(time(1:3)),'InputFormat','dd-MM-yy HH:mm')
or
datetime(string(time(1:3)),'InputFormat','yy-MM-d HH:mm')
if it is 2013 in you example. It is not quite clear from the dates.

How can I convert one date and time from two colums?

Im trying to convert the first two columns of a cell into a Matlab time. First column {1,1} is the date in YYYY-MM-DD format and the second is the time in HH:MM format.
Any ideas where I'm going wrong? My code:
file = 'D:\Beach Erosion and Recovery\Bournemouth\Bournemouth Tidal
Data\tidal_data_jtide.txt'
fileID = fopen(file);
LT_celldata = textscan(fileID,'%D%D%D%D%d%[^\n\r]','delimiter',',');
formattime = 'yyyy-mm-dd HH:MM'
date = LT_celldata{1,1};
time = LT_celldata{1,2};
date_time = datenum('date','time'); code
Screenshot below is LT_celldata{1,1} :
You can combine variables date and time with the following code:
date = datetime(LT_celldata{1,1},'InputFormat','yyyy-MM-dd');
time = datetime(LT_celldata{1,2},'InputFormat','HH:mm:ss','Format','HH:mm:ss');
myDatetime = datetime(date + timeofday(time),'Format','yyyy-MM-dd HH:mm:ss');
The code uses timeofday function to combine date and time information from the two different variables. You may find more information and examples at this documentation page.

Time conversion to Epoch seconds

Is there a way to convert time= 08/10/2014 23:34:02 to Epoch seconds (array of numbers) in MATLAB?
So you want the Unix standard which can be calculated as follows:
InputDate=datenum('20141008 233402','yyyymmdd HHMMSS');
UnixOrigin=datenum('19700101 000000','yyyymmdd HHMMSS');
EpochSecond=round((InputDate-UnixOrigin)*86400);
>> 1412811242
EDIT for the OP's date format:
MYSTRING = '08/10/2014 23:34:02';
InputDate = datenum(MYSTRING,'dd/mm/yyyy HH:MM:SS');
UnixOrigin=datenum('19700101 000000','yyyymmdd HHMMSS'); %//This can stay the same, unless you want to change it for consistency.
EpochSecond=round((InputDate-UnixOrigin)*86400);
>>1412811242

Matlab: Convert file date with milliseconds into Matlab time format

I have some text files with dates of the form
2015-09-08 14:38:03
2015-09-08 14:38:03.1
2015-09-08 14:38:03.2
that I want to convert into Matlab date/time format. As you can see, the text strings have a different time format regarding the milliseconds. In the first case, no milliseconds are given; in the seconds case, milliseconds are given with one digit only. This gives a sampling rate of 20Hz for measuring data.
So far, only
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS');
is working, but of course misses the milliseconds. A conversion like
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
does not work as the milliseconds are either zero (full seconds) or have one digit after the '.' delimiter. A workaround like
x = datestr(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
i.e. converting the text string into a Matlab string (and giving it additional FFF/FF digits) and then converting that one into a date/time number works - but this is such time-cosuming that I cannot use it for my data. I have millions of time rows in different files.
Do you have any ideas?
Greetings, Arne
Thanks to Nick I found a way to solve it:
dataVelPres = readtable(fileName, ...
'ReadVariableNames', false, ...
'HeaderLines', 4 );
dataVelPres.Properties.VariableNames = {'date' 'velU' 'velV' 'velW' 'pres'};
dateMill = datetime(dataVelPres.date, 'inputformat', 'yyyy-MM-dd HH:mm:ss.S');
dateFull = datetime(dataVelPres.date, 'inputformat', 'yyyy-MM-dd HH:mm:ss');
dateNaT = isnat(dateMill);
dateMill(dateNaT) = dateFull(dateNaT);
dataVelPres.dateTime = dateMill;
dataVelPres.date = datenum(dataVelPres.dateTime); % Convert to number format if needed
This works with two tables - one for millisec. and one without - and puts both together, as both give NaT entries in case the inputformat does not match.
Is there a more elegant way?
You may try something like:
a='2015-09-08 14:38:03';
s=strsplit(a,{'-',':',' '})
x=datenum(cellfun(#str2num,s(1:end)))
I highly recommend using the new datetime object:
strings = {'2015-09-08 14:38:03', '2015-09-08 14:38:03.1', '2015-09-08 14:38:03.2'};
dates = {};
for d = strings
d = d{1};
try
dt = datetime(d, 'inputformat', 'yyyy-MM-dd HH:mm:ss.S');
catch
dt = datetime(d, 'inputformat', 'yyyy-MM-dd HH:mm:ss');
end
dates{end + 1} = dt;
end
>> dates
dates =
[08-Sep-2015 14:38:03] [08-Sep-2015 14:38:03] [08-Sep-2015 14:38:03]
>> dates{end}.Second
ans =
3.2000
It's also easy convert from a datetime object to a datenum:
>> x = [datetime('now'), datetime('yesterday')]
x =
10-Dec-2015 12:53:40 09-Dec-2015 00:00:00
>> datenum(x)
ans =
1.0e+05 *
7.3631 7.3631
>>

How to work with Unix timestamps in Matlab?

I have some data files with Unix timestamps (in this case, number of milliseconds since Jan 1, 1970 00:00 UTC). I would like to convert these to human-friendly date/time strings (e.g. 31-Aug-2012 11:36:24) in Matlab. Is there an easy way to do this in Matlab, or am I better off using an external library (e.g. java.text.SimpleDateFormat)?
How about
date = datestr(unix_time/86400 + datenum(1970,1,1))
if unix_time is given in seconds, unix_time/86400 will give the number of days since Jan. 1st 1970. Add to that the offset used by Matlab's datenum (datenum(0000,1,1) == 1), and you have the amount of days since Jan. 1st, 0000. This can be easily converted to human-readable form by Matlab's datestr.
If you have milliseconds, just use
date = datestr(unix_time/86400/1000 + datenum(1970,1,1))
Wrapped in functions, these would be
function dn = unixtime_to_datenum( unix_time )
dn = unix_time/86400 + 719529; %# == datenum(1970,1,1)
end
function dn = unixtime_in_ms_to_datenum( unix_time_ms )
dn = unix_time_ms/86400000 + 719529; %# == datenum(1970,1,1)
end
datestr( unixtime_to_datenum( unix_time ) )
Newer versions of MATLAB (verified in R2015a) have a datetime type that is useful for working with and formatting dates and times. You can convert UNIX timestamps into a MATLAB datetime with
dt = datetime( unix_time, 'ConvertFrom', 'posixtime' );
and then use datestr as before for formatting as a string
datestr( dt )