How to I parse an array of time stamp strings read into a table form a CSV file?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Les Beckham
2024-3-1
编辑:Les Beckham
2024-3-1
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'
curr = table(times, rand(12, 1), 'VariableNames', {'Timestamp', 'Current'}) % Example table
secs = timeofday(curr.Timestamp)
secs = seconds(secs - secs(1))
class(secs)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!