Convert time dates to seconds
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Danilo M
il 22 Nov 2016
Commentato: Star Strider
il 23 Nov 2016
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 Commenti
Risposta accettata
Star Strider
il 22 Nov 2016
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 Commenti
Star Strider
il 23 Nov 2016
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.
Più risposte (1)
Peter Perkins
il 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!