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!