How to I parse an array of time stamp strings read into a table form a CSV file?

1 visualización (últimos 30 días)
I have time stamped data collected into a csv file which I bring into Matlab as table.:
curr=
1053×2 table
Time Current
_________________________ ____________________________
"02/27/2024, 12:03:25 PM" -0.329
"02/27/2024, 12:03:30 PM" -0.14
"02/27/2024, 12:03:35 PM" -0.0765
"02/27/2024, 12:03:40 PM" -0.182
"02/27/2024, 12:03:45 PM" -0.292
I ultimately want to change the times into durations to plot the data vs. relative time; for example, the first entry would be time 0, the second would be 5 seconds later, etc...
I've tried something like this.:
time = split(curr.Time(:,1),'P')
time = split(time(:,1),',')
time = strip(time(:,2))
time = char(time(1:end-1))
time = duration(string(time(:,1:end-1)))
at this point I'm left with an array of duraitons. If I then subtract the first entry, I get the durations:
secs = time
secs = secs- secs(1)
However, because I got rid of the PM, I end up with negative seconds when the hour changed from 12 to 1. So, I tried and "if statement" to add 12 to any hour less than 12, but I couldn't do it.
if hour(string(time_curr_NHP))<12
hour(string(time_curr_NHP)) = hour(string(time_curr_NHP))+12
end
I'm pretty sure I've complicated this a lot more than it needs to be! There has to be a much simpler way to do this. I'd appreciate any help.
Thank you.

Respuesta aceptada

Les Beckham
Les Beckham el 1 de Mzo. de 2024
Editada: Les Beckham el 1 de Mzo. de 2024
Don't operate on the timestamps as strings, use the datetime and duration types and the associated functions.
times = linspace(datetime(2024, 3, 1, 11, 30, 0), datetime(2024, 3, 1, 12, 25, 0), 12)';
times.Format = 'MM/dd/uuuu hh:mm:ss a'
times = 12×1 datetime array
03/01/2024 11:30:00 AM 03/01/2024 11:35:00 AM 03/01/2024 11:40:00 AM 03/01/2024 11:45:00 AM 03/01/2024 11:50:00 AM 03/01/2024 11:55:00 AM 03/01/2024 12:00:00 PM 03/01/2024 12:05:00 PM 03/01/2024 12:10:00 PM 03/01/2024 12:15:00 PM 03/01/2024 12:20:00 PM 03/01/2024 12:25:00 PM
curr = table(times, rand(12, 1), 'VariableNames', {'Timestamp', 'Current'}) % Example table
curr = 12×2 table
Timestamp Current ______________________ ________ 03/01/2024 11:30:00 AM 0.90385 03/01/2024 11:35:00 AM 0.13638 03/01/2024 11:40:00 AM 0.99377 03/01/2024 11:45:00 AM 0.025041 03/01/2024 11:50:00 AM 0.11393 03/01/2024 11:55:00 AM 0.43305 03/01/2024 12:00:00 PM 0.77071 03/01/2024 12:05:00 PM 0.92144 03/01/2024 12:10:00 PM 0.28531 03/01/2024 12:15:00 PM 0.29388 03/01/2024 12:20:00 PM 0.78679 03/01/2024 12:25:00 PM 0.49889
secs = timeofday(curr.Timestamp)
secs = 12×1 duration array
11:30:00 11:35:00 11:40:00 11:45:00 11:50:00 11:55:00 12:00:00 12:05:00 12:10:00 12:15:00 12:20:00 12:25:00
secs = seconds(secs - secs(1))
secs = 12×1
0 300 600 900 1200 1500 1800 2100 2400 2700
class(secs)
ans = 'double'
  2 comentarios
Jorge
Jorge el 4 de Mzo. de 2024
I appreciate the detailed answer demonstrating how to use the datetime and duration types and the associated functions. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by