Convert time dates to seconds
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Peter Perkins
le 23 Nov 2016
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.
Catégories
En savoir plus sur Calendar dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!