Converting serial date numbers to datetime array

Hello!
I'm sorry for bothering you all, but I'm really new to MatLab and trying to learn something new to help me with my research.
I've recently made som measurements and my outputs in txt shows time stamp in format "3718505030". I understood that this is MatLab Serial Data format, but I need to convert them into conventional data and time format. Probably the date is not included in this information. I've tried function
t = datetime(.3718505035,'ConvertFrom','datenum')
but it only converts to format HH:MM:SS, but I really need it in HH:MM:SS:SSS and also I need to apply the function on multiple rows.
Can you help me with this issue?
Thank you.

Answers (1)

>> t = datetime(.3718505035,'ConvertFrom','datenum','Format','HH:mm:ss.SSS')
t =
08:55:27.883
"also I need to apply the function on multiple rows"
>> V = [0.1;0.3;0.7];
>> t = datetime(V,'ConvertFrom','datenum','Format','HH:mm:ss.SSS')
t =
02:24:00.000
07:12:00.000
16:48:00.000

9 Comments

Hi!
Thanks for you answer. I understand now how to use.
If I may, I have another further question - when I import my data into the matlab, all numbers are shown as 3.7185e+09 and when I enter the function as you sent me, all outputs are rubbish.
"all numbers are shown as 3.7185e+09"
In your question you showed us that the data is "3718505030" and this should be interpreted as a decimal fraction, which you gave as datetime(.3718505035,..).
Now you have shown that that data is not a decimal fraction at all, but in fact has a magnitude 1e9. Quite different, so of course the ouputs are rubbish! Rubbish in -> rubbish out.
If the data should be interpreted as a decimal fraction of a day (e.g. as a serial date number), then you would need to convert it to a fraction first.
However it is quite possible that your data is actually a unix timestamp or some other encoding (rather than a MATLAB serial date number), in which case 3718505030 represents the date+time 2087-11-01T06:03:50 or whatever it encodes (if this is the case then pick the appropriate input option for datetime). Of course I cannot tell you what the date encoding is (they are just numbers), YOU need to know that based on the documentation of the data that you have. But until then, you can expect that rubbish in -> rubbish out.
Numbers in the range 3.7E9 are not serial date numbers. However, values in that range are plausibly seconds since Jan 1, 1900, leading to dates approximately November 1, 2017.
Note that seconds since Jan 1, 1900 is not used by Excel (it uses days since Jan 1, 1900), and is also not used by POSIX (it uses seconds since Jan 1, 1970, or sometimes microseconds since then.)
datetime(1900,1,1) + seconds(3.7185e+9)
@Walter Roberson: well spotted. Can also be converted directly using datetime:
>> t = datetime(3718505030,'ConvertFrom','epochtime','Epoch','1900-01-01')
t =
01-Nov-2017 06:03:50
Peter Marienka's "Answer" moved here and formatted properly:
Hi,
when I type this command:
t = datetime (.3718505030, 'ConvertFrom','datenum','Format','HH:mm:ss.SSS')
into command line, the output time is 08:55:27.883, ant that's actually right! So my data is for matlab, but now I have to figure how to import them as
.3718505030
.3718505035
.3718505040
etc.
"...but now I have to figure how to import them as..."
That depends entirely on the file format, whether the number of digits changes, what tool/s you are using to import that file data, etc., which you have not told us about. However if you have a string/character vector of the digits of a decimal fraction, then it is easy to convert to numeric:
>> str2double(strcat('0.','123'))
ans =
0.123
>> str2double(strcat('0.',{'123';'456';'789'}))
ans =
0.123
0.456
0.789
t = days(YourVector/1e10);
t.Format = 'hh:mm:ss.SSS';
No, the user is treating the values as datenum and those are days since 1 jan 0 CE so using days() is appropriate
Walter's right: if .3718505030 is supposed to be interpreted as "a little less than 9 hours," then using days to create a duration is the correct thing. That was one of the flaws of datenums: is it a long elapsed time, or an absolute date during the dark ages? The datetime/duration pair solves that problem.
I can't tell if it's days(.3718505030) or days(3718505030/1e10), but same difference.

Sign in to comment.

Products

Release

R2019a

Asked:

on 26 Apr 2019

Commented:

on 3 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!