How to sort by date
39 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have two data in two matrix (Matrix A: my Table1, MatrixB is my Table 2) as below. But the data is not in the order of date, and I want to arrange data in the correct order of date, arrange the the data in Table2 accordingly. Please kindly help some one, How to sort by date,
Table1:
2014/8/1 11:32
2014/8/2 8:53
2014/8/3 1:32
2014/8/4 6:42
2014/8/5 0:14
2015/1/31 11:41
2015/2/1 1:02
2014/11/9 11:29
2014/12/31 1:02
2015/1/1 11:34
2015/2/2 1:51
2015/2/3 1:43
2015/1/2 1:02
2014/8/7 1:02
2014/9/8 11:02
2014/8/6 10:19
Table2:
0.26
0.00
0.46
0.57
0.21
0.22
0.41
0.76
0.20
0.33
0.56
0.23
0.28
0.25
0.44
0.51
Many many Thanks in advance.
1 comentario
islam helmy
el 8 de Feb. de 2017
the easy way to do that, is to express the date in days, I.e.date=year*365+month*30+day. then sorting them, and use index as dicuss in previous comments but unfortunately if you have files with the same date need to do next step. the next step is to get only the equal date, then express their time in seconds, I.e. time=hour*365+minuts*60+seconds, finally sorted them
Respuestas (2)
Star Strider
el 14 de Feb. de 2015
One approach:
[d,s,r] = xlsread('SortingbyDate.xlsx'); % Import Data
dn = datenum(s(2:17)', 'mm/dd/yyyy HH:MM:SS AM') % Convert To Date Number
[dns,sidx] = sort(dn); % Sort By Date, Return Indices
SortData = [dns, d(sidx)] % Sorted Data
I kept the dates as date numbers in the SortData array because I prefer them that way. You can always convert them to other formats. but that would require a cell array. This keeps them all numeric for convenience.
2 comentarios
Star Strider
el 14 de Feb. de 2015
It worked for me in R2014b as posted. There could be version differences. If you’re not using R2014b, we may have to experiment online to get it running in your version.
What size is ‘s’ when you run it? It should be a column vector.
Try this:
dn = datenum(s(2:17,1), 'mm/dd/yyyy HH:MM:SS AM'); % Convert To Date Number
It also works for me.
I test the code I post here if I possibly can. If I can’t test it, I label it as ‘untested code’.
Peter Perkins
el 9 de Feb. de 2017
Editada: Stephen23
el 9 de Feb. de 2017
Unless you are using a version of MATLAB older than R2014b, use datetime and tables:
>> time = {'2014/8/4 6:42';'2014/8/5 0:14';'2015/1/31 11:41';'2015/2/1 1:02';'2014/11/9 11:29';'2014/12/31 1:02'};
>> x = (1:6)';
>> time = datetime(time,'InputFormat','yyyy/MM/dd HH:mm');
>> t = table(time,x)
t =
time x
____________________ _
04-Aug-2014 06:42:00 1
05-Aug-2014 00:14:00 2
31-Jan-2015 11:41:00 3
01-Feb-2015 01:02:00 4
09-Nov-2014 11:29:00 5
31-Dec-2014 01:02:00 6
>> t = sortrows(t,'time')
t =
time x
____________________ _
04-Aug-2014 06:42:00 1
05-Aug-2014 00:14:00 2
09-Nov-2014 11:29:00 5
31-Dec-2014 01:02:00 6
31-Jan-2015 11:41:00 3
01-Feb-2015 01:02:00 4
0 comentarios
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!