Convert time dates to seconds
显示 更早的评论
I have a temporal series matrix from some hydrological stations like this:
yy mm dd hh mm p q
2010 01 01 00 00 0.0 1.4
2010 01 01 00 10 0.0 1.4
2010 01 01 00 20 0.0 1.3
...
And I need to create a matrix with date in seconds like this:
ss p q
0 0.0 1.4
600 0.0 1.4
1200 0.0 1.3
...
But the stations has measurement errors, sometimes the interval is greater than 10 minutes, so I can't just create a sequencie like (0:600:endline). There's some way to calculate the seconds in matlab?
采纳的回答
更多回答(1 个)
Peter Perkins
2016-11-23
You might find this easy to do using a table and datetimes/durations. Given a csv file that look like your example,
>> t = readtable('stations.dat');
>> t.elapsed = datetime(t.yy,t.mm,t.dd,t.hh,t.mm_1,0) - '1-Jan-2010';
>> t = t(:,{'elapsed' 'p' 'q'});
>> t.elapsed.Format = 's'
t =
elapsed p q
________ _ ___
0 sec 0 1.4
600 sec 0 1.4
1200 sec 0 1.3
If your elapsed time field absolutely has to be a numeric value, replace the format line with
t.elapsed = seconds(t.elapsed;
Although I didn't show it, using datetimes admits the possibility that your data are local timestamps that involve DST shifts.
If you have access to R2016b, you should look into using timetables.
类别
在 帮助中心 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!