MATLAB - working with timestamps - matlab

How can I convert this kind of data 08:00:43.771 given as string into a number specifying the number of milliseconds since midnight corresponding to this time instance?

I generally use the Matlab datenum outputs for timestamping in Matlab. Datenums are the number of days since 0/0/0000, expressed as a double (double precision numbers are precise to about 14 usec for contemporary dates).
Using datenums.
currentDateTime1 = datenum('08:00:43.771'); %Assumes today
currentDateTime2 = datenum('6/8/1975 08:00:43.771'); %Using an explicit date
millisecondsSinceMidnight = mod(currentDateTime1 ,1) *24*60*60*1000; %Mod 1 removes any day component
millisecondsSinceMidnight = mod(currentDateTime2 ,1) *24*60*60*1000; %Then this is just a unit conversion
For unusual string formats, use the extended form of datenum, which can accept a string format specifier.

Use 1000*etime(datevec('08:00:43.771'),datevec('0:00')) to give the number of milliseconds since midnight. etime gives the number of seconds between two date vectors, datevec converts strings to date vectors (assuming Jan 1 this year if only a time is given).

Related

Converting Date Time into standard format

I have data (240 x 9 dimensions) in which a DATETIME column has entries as shown below. I want to separate the date and time parts. Also, I need to calculate the time difference between an entry and a previous one. The date is formatted as yyyymmdd followed by the hhmmsssss. However, the last three entries of the seconds represent the decimal seconds i.e., 36200 means 36.2 seconds
Using MATLAB I have separated the date and time entries as character arrays as the original data was in the double format as below:
datestr: 240 x8 char e.g., '20190101'
timestr: 240 x9 char e.g., '001634200'
I want to convert the date into the standard format. I used
formatOut = 'yyyy/mm/dd';
date=datestr(datenum('20190101','yyyymmdd',1900),formatOut);
But getting this error : Index in position 1 exceeds array bounds (must not exceed 240).
Additionally, I need to convert the time into hh:mm:ss.sss format and then calculate the difference between a time and its previous entry.
Any help will be appreciated.

Is there an easy way to read integer time data from a netcdf file using the time-attribute?

I am trying to read time-coordinate data from a netCDF file using matlab. I have a netCDF file (which I created) that has a time variable in the format of a double corresponding to the number of hours from a specific time (see below).
Variable attributes:
double time(Time) ;
time:standard_name = "Time" ;
time:units = "hours since 2002-01-01 0:0:0" ;
time:calendar = "proleptic_gregorian" ;
When I read the time variable using ncread) into matlab, it just prints out an integer e.g.,1. However, if I use "ncdump" to explore the file, I see the time variable in it's coordinate time e.g., 2002-01-01 01.
Specifically: "ncdump -t -v time ncfile.nc"
I'm relatively new to matlab, and I was wondering if anyone knew if there was a similar, or an equally simple, way to read this time variable as its coordinate time into matlab, either as a string, or numerical date. Specifically, I would like to avoid having to parse the attribute string and code up a bunch of pointers and conditions to convert the integer data to an actual date.
Alternatively, should I just create a new time variable in these files that is just an array of dates as strings?
Any information is very much appreciated!
Thanks!
NetCDF stores time as an offset from an epoch. From your variable attribute, your epoch is 2002-01-01 0:0:0, and the time is hours since then. Matlab has a similar methodology called date numbers, although it is based off of days since an epoch (which they call pivot years). There are two functions that you should look into: datenum and datestr. The first converts a string into a date number and the other converts a date number into a date string.
You can convert your time variable into a compatible Matlab date number by dividing by 24 and then use the datestr function to format it however you like. Here is a simple example:
>> time = [1;2;3;4];
>> datestr(time./24+datenum('2002-01-01 0:0:0'))
ans =
01-Jan-2002 01:00:00
01-Jan-2002 02:00:00
01-Jan-2002 03:00:00
01-Jan-2002 04:00:00
Look at the Matlab help files associated with the two functions and you can format the date output however you like.

How to convert UNIX timestamp into EST time in Matlab?

How can I convert UNIX time stamp to system time and date? In my case EST time.
Unix timestamps are often in the form of millisecs since midnight 1/1/1970, but sometimes in the form of secs since the same moment. Converting them to a Matlab datenum can be done via datenum([1970 1 1 0 0 timestamp/1000]) (see related Mathworks tech-note). In case the timestamp is not in millisecs but rather in secs, don't divide by 1000. Once you get the datenum, you can use the datestr function to convert into a string in any format you like.
Alternately, you can use Java's java.util.Date(timestamp) to convert the timestamp into a Java date object, then use the Date methods to convert into a Java string, and then use Matlab's char function to convert it into a Java string.
You can use datenum to convert almost any format time string into a serial date number, then use datestr or datevec to convert to whatever format you need.
I just had the same problem and helped myself with a function like
function dv=datevec_from_timestamp(ts)
% Converts a UNIX timestamp (UTC based) to a (local!) datevec which can
% then be used as usual with datestr, datenum etc.
cal=java.util.Calendar.getInstance;
cal.setTimeInMillis(ts * 1000)
dv = [cal.get(cal.YEAR) cal.get(cal.MONTH)+1 cal.get(cal.DAY_OF_MONTH) ...
cal.get(cal.HOUR) cal.get(cal.MINUTE) cal.get(cal.SECOND)];

How to use DateTime or FileTime date types in Matlab

I would like to parse/convert DateTime (or FileTime) data type which is int64 and measured in "ticks" which are 100-nanoseconds units, to a datenum or a better date & time format in Matlab.
I must use a counter with a high resolution 64bit as the difference between time stamps can be a couple of microseconds and the "spacing" isn't even.
Using latest Matlab version, the "ticks" variable is read as a string..
Any recommendations?
Thank you for your help
Datenums probably won't represent this effectively. They are doubles, an approximate type, with 1.0 = 1 day; the resolution limit for recent dates is about 10 microseconds. Also, the datenum related functions aren't written for high precision processing, and probably won't work well here.
>> datenum_precision = eps(now)
datenum_precision =
1.1642e-010
>> millisecond = 1.0/(24*60*60*1000)
millisecond =
1.1574e-008
>>
You're probably better off creating your own class that wraps the "ticks" as uint64 and provides methods for converting to human-readable presentation (like datestr()), doing addition and subtraction, parsing the strings you're getting to the uint64 values, and so on. Something with an interface like this.
classdef filetime
properties
tick; % An array of uint64 ticks
end
methods (Static = true)
out = parse(str); % Parse strings to #filetime
end
methods
out = filetimestr(obj); % human-readable strings
out = minus(a,b);
out = plus(a,b);
out = sort(obj);
out = datenum(obj); % maybe a lossy conversion to datenum, for convenience
end
end
You'll need R2010b to do arithmetic on 64-bit ints; older versions don't support it.
The tricky part would be if you wanted to do plots using the filetimetick objects; the Handle Graphics plots don't support user-defined objects.
You can convert your numbers to MATLAB's serial date with a simple equation:
step = 1e-7; % 100 nanosec
ticks = int64(250000);
timenum = double(ticks)*step/24/60/60;
First, for very large integer values converting to double might loose precision. If this is the case you probably better to stay with your numbers without converting to serial date.
Second, DATESTR/DATETICK functions support precision up to milliseconds. Try:
datestr(timenum,'HH:MM:SS.FFF')
So, think about it and please answer Andrew's comment - what do you need this conversion for?
D is a uint64 value of 100 ns since 01-01-1601, the result must be a datenum. This means days passed since 00-Jan-0000.
step = 100E-9; % 100 nanoseconds
step = step/(24*60*60); %fraction of a day
D = datenum('01-01-1601')+double(D)*step;
%D is now a datenum
Modern version using datetime (matlab >2014b)
%uint64 value of 100 ns since 01-01-1601
step = 100E-9; % 100 nanoseconds
step = step/(24*60*60); %fraction of a day
D = datetime('01-01-1601')+days(double(D)*step);
As mentioned above, both methods lose some of the accuracy that we had. In the case of datetimes this can probably be circumvented. It happens at double(D)*step because D is big for current dates. Many nanoseconds have passed since 1601. By converting D to a more recent offset one could get a more accurate conversion.

How can i interpret a time value in ascii into a numerical value?

I have a file which is as follows:
15:03:21 II 0.88 0.64
15:03:31 II 0.88 0.64
15:03:42 II 0.40 0.40
etc.
after loading the file in matlab, I want to be able to read the first column (which corresponds to time) and interpret them as numerical values. At the moment, they are interpreted as a string of ascii characters and i can't perform any mathematical operations on them. Does anyone have any suggestions as to how i can read the time as numbers instead of a string of ascii characters?
Use DATENUM and DATEVEC to convert your date strings to useful numeric values.
Like Jonas said, but more specifically,
t = {'15:03:21 ' '15:03:31 ' '15:03:42 '};
datenum(t, 'HH:MM:SS')
In addition to above answers there is another useful function DATEVEC which converts either date string or datenum output to vector of year-month-day-hours-minutes-seconds. Try it out:
tvec = datevec(t)
Notice that if there is only time in the string, the date will be January 1st of the current year. You can always cut it out with
tvec(:,1:3) = [];