Convert double to cell array

I've read some data from a text file using textscan and managed to create a 1x6 cell array, the first column of which includes a time stamp, though not in the format I require. Example:
1643201283507717200
The translation of this is 16:43:20.128... to 13 decimal points.
Can anyone advise on how I might convert this to an actual time stamp, ideally HH:MM:SS without losing any of the precision, and applying this to every value in the column. For reference, Ill be looking to calculate the time delta from the first value
Thank you

 Accepted Answer

You are going to run into some precision errors with a number that large.
format long
t = 1643201283507717200;
t-1643201283507717000 % result should be 200
ans =
0
I think it would be best to go back to the original textscan operation and implement changes there to capture this as separate HH, mm, ss, and ms.
t = '1643201283507717200';
T = textscan(t,'%2f %2f %2f %f')
T = 1×4 cell array
{[16]} {[43]} {[20]} {[1.283507717200000e+12]}
T{4} - 1283507717199 % result should be 1
ans =
1
You will still have the challenge of capturing milliseconds accurately to 13 decimal places, but at least here all the numbers are still correct.
You could then build up a time vector. However, you are again limited to just 9 decimal places, and you will again not have 13 decimal places of precision. This has to do with how floating point numbers are stored digitally.
tm = duration(T{:,1},T{:,2},T{:,3},T{:,4}*1e-10,'Format','hh:mm:ss.SSSSSSSSS')
tm = duration
16:43:20.128350771
milliseconds(tm - duration(16,43,20,128.3507717000)) % answer should be 2e-8
ans =
1.490116119384766e-08
I guess the first question, then, is is your timestamp format correct, and if so, is it truly accurate to that number of decimal places? If so, then datetimes or durations may not be the appropriate choice for you.

3 Comments

Hi thanks for the response, I had looked into the datetime function originally but this wouldn't retain the precision. All I am interested in the relative time delta between measurements, so one way around this is to calculate the absolute number of seconds from a reference, i.e. midnight. I also think that I could lose two decimal places (so 11 in total) as my sensor is not capable to 13 decimal places.
E.g. 1643201283507717200
20.12835077172 + (43*60)+(16*60*60) = 1.203801283507717e+05
So for every cell in the time cell array, I would need to calculate absolute no. secs (from a reference) based on the HH:MM of the time entry. Any ideas on how I would do this?
Thank you
The problem with converting the entire timestamp to seconds is you end up making your number even larger. If it is just the that is important, I think I would do the following.
  1. Modify the original textscan to read in the time as hh, mm, ss, ms as shown previously.
  2. Convert the hh,mm,ss into a duration
  3. Subtract the first duration from all durations, and first ms from all ms.
  4. Find an approproate way to combine the duration with the ms (can add them if both are scaled correctly)
Please keep in mind this is a contrived example since you haven't shared your data. You are still dealing with some very small numbers
format long
t = '1643201283507717200 1643201283507717300';
T = textscan(t,'%2f %2f %2f %13f')
T = 1×4 cell array
{2×1 double} {2×1 double} {2×1 double} {2×1 double}
ms = T{:,4}
ms = 2×1
1.0e+12 * 1.283507717200000 1.283507717300000
tm = duration(T{:,1:3},'Format','hh:mm:ss')
tm = 2×1 duration array
16:43:20 16:43:20
dtm = milliseconds(tm-tm(1))
dtm = 2×1
0 0
dms = ms-ms(1)
dms = 2×1
0 100
dt = dtm + milliseconds(dms*1e-10)
dt = 2×1 duration array
0 sec 1e-11 sec
Thank you kindly. This makes sense

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!