convert day year hour data series with missing data to a serial number

I have a year of data from "01/01/2003 00:00" to "2003/12/31 23:59" with some days/hous missing from the data series. I think to convert these dates/times to serial numbers I have to use the datenum. To convert each one of these numbers I understand that I have to do: datenum(2003,1,1,0,0,0) which gives ans =
731582.00
This is fine for one number but how I can convert the whole series? thanks, Katerina

 Risposta accettata

f = fopen('dates1.txt');
c = textscan(f,'%s %s','collectoutput',1);
fclose(f);
out = datenum(strcat(c{1}(:,1),{'_'},c{1}(:,2)),'dd/mm/yyyy_HH:MM');
or
f = fopen('dates1.txt');
c = textscan(f,'%s','delimiter','\n');
fclose(f);
out = datenum(c{1},'dd/mm/yyyy HH:MM')

4 Commenti

I would like to ask something additional to my previous question.
If now I have a file with a full year, as it is in the text file attached (one column months, one days, one hours), and I try to apply the above to convert to serial number, then the comment out = datenum(c{1},'dd/mm HH:MM') does not give a result (probably because the format is diffent)
What code I should apply in this case in order to convert the series to a serial number?
thanks, K
Let your year: year1 = 2014
dt = datenum([2014 1 1 0 0 0;2014 12 31 23 0 0]);
out = (dt(1):1/24:dt(2))';

Accedi per commentare.

Più risposte (1)

str{1} = '01/01/2003 00:00'
str{2} = '2003/12/31 23:59'
datenum( str )
ans =
1.0e+05 *
7.3158
7.3195
So you should just be able to put all your strings into a cell array and pass that in to datenum

5 Commenti

Adam
Adam il 22 Ago 2014
Modificato: Adam il 22 Ago 2014
I've never really used datanum, but it seems it doesn't mind if your array of strings is jumbled up with different formats either. I just pasted in the two dates from your question without realising they were in different formats!
Edit : Looking at the help I guess it is just slower with jumbled up format names that the Matlab function has to work out itself for each element. Putting them all in the same format and specifying that explicitly would be faster.
Hi Adam, Thanks for this but still do not know how do I do this. How do I convert all the series that I have?
I is a year long series, I cannot do it one by one, this is my question.
Could you please show me the code if you know?
Say it is as shown below:
31/01/2003 16:00 31/01/2003 17:00 31/01/2003 18:00 31/01/2003 19:00 31/01/2003 20:00 31/01/2003 21:00 31/01/2003 22:00 31/01/2003 23:00 01/02/2003 02:00 01/02/2003 03:00 01/02/2003 04:00 01/02/2003 05:00 01/02/2003 06:00 01/02/2003 07:00 01/02/2003 08:00 01/02/2003 09:00 01/02/2003 10:00 01/02/2003 14:00 01/02/2003 15:00 01/02/2003 16:00 01/02/2003 17:00 01/02/2003 18:00 01/02/2003 19:00 01/02/2003 21:00
Is that all one string in a single variable?
It is one column of data. Each line in the column is for example: 31/01/2003 16:00
If it is a cell array you should just be able to pass it to datenum. If not then you will need to convert each row to a cell containing the string rather than a standard char matrix using e.g.
dateCells = cellstr( dates )

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by