Convert double to cell array

21 visualizaciones (últimos 30 días)
Daniel Rowe
Daniel Rowe el 28 de En. de 2022
Comentada: Daniel Rowe el 29 de En. de 2022
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

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 28 de En. de 2022
Editada: Cris LaPierre el 28 de En. de 2022
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 comentarios
Cris LaPierre
Cris LaPierre el 28 de En. de 2022
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
Daniel Rowe
Daniel Rowe el 29 de En. de 2022
Thank you kindly. This makes sense

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by