datevec Matlab skips time 00:00:00 - matlab

I have an array of timestamps, these are just three examples:
time_1 = '23/01/2013 00:47:00'
time_2 = '04/02/2013 00:00:00'
time_3 = '27/01/2013 03:31:00'
I want to use 'datevec' on these timestamps:
tVec_1 = datevec(datenum(time_1,'dd/mm/yyyy HH:MM:SS'));
tVec_2 = datevec(datenum(time_2,'dd/mm/yyyy HH:MM:SS'));
tVec_3 = datevec(datenum(time_3,'dd/mm/yyyy HH:MM:SS'));
for the tVec_1 and tVec_3 timestamp it works nicely, but it crashes for the tVec_2:
Error using dtstr2dtvecmx
Failed on converting date string to date number.
Error in datevec (line 117)
y = dtstr2dtvecmx(t,icu_dtformat);
I suspect, that the problem comes from "zero time" which is not treated properly by my format 'dd/mm/yyyy HH:MM:SS' (if I remove the last "HH:MM:SS" then it works).
Unfortunately, I could not find anything useful on matlab website.
Suggestions are welcome!
Thanks!

Related

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
>>

Getting datenum of predefined days, Matlab

I can get datenum of today
Daynum=datenum(fix(clock))
But how can I get it
yesterday
7 days earlier
6-11-2015
I need all three above listed days. I know some of these are already integrated in new version but I don't have Matlab 2015.
The output of datenum is:
A serial date number represents the whole and fractional number of
days from a fixed, preset date (January 0, 0000).
So to get yesterday you could do:
Daynum_yesterday = datenum(fix(clock)) - 1;
And 7 days ago would be:
Daynum_7days = datenum(fix(clock)) - 7;
If you have a specific date you can already pass it to datenum, with an optional format specifier:
Daynum_mydate = datenum('6-11-2015');
% or
Daynum_mydate = datenum('6-11-2015', 'mm-dd-yyyy');
Which return the same result.
We can test all of these using datestr:
str_yesterday = datestr(Daynum_yesterday);
str_7days = datestr(Daynum_7days);
str_mydate = datestr(Daynum_mydate);
Which returns:
str_yesterday =
16-Nov-2015 07:44:41
str_7days =
10-Nov-2015 07:44:41
str_mydate =
11-Jun-2015
Edit: And an obligatory Falsehoods Programmers Believe About Time citation.

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 )