Convert time dates to seconds
5 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Star Strider
2016 年 11 月 22 日
Use the etime function:
t = [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];
td = etime([t(:,1:5) zeros(size(t,1),1)], [t(1,1:5) 0]);
Output = [td t(:,6:7)]
Output =
0 0 1.4
600 0 1.4
1200 0 1.3
You need to fill out the 5-element date vectors with an additional zero, then use etime to calculate the elapsed time in seconds.
2 件のコメント
Star Strider
2016 年 11 月 23 日
My pleasure!
My code works with the data you posted. I would need your file to determine the reason it did not work with your complete data. You should be able to use the matrix you imported with my code to calculate all the lines at once. (I am using R2016b, so there may be version differences with etime, but I suspect that is not the problem.)
If each station matrix is in a separate file, see: How can I process a seqeunce of files? for details on how to process them.
Please post one of your files (or a representative part of one), and I will see if I can determine what the problem is.
その他の回答 (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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!