How to sort by date

40 次查看(过去 30 天)
Mekala balaji
Mekala balaji 2015-2-14
编辑: Stephen23 2017-2-9
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 个评论
islam helmy
islam helmy 2017-2-8
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

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2015-2-14
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 个评论
Mekala balaji
Mekala balaji 2015-2-14
The following erros:
??? Error using ==> datenum at 182 DATENUM failed.
Error in ==> SortingbyDate at 5 dn = datenum(s(2:17)', 'mm/dd/yyyy HH:MM:SS AM'); % Convert To Date Number
Caused by: Error using ==> dtstr2dtnummx Failed on converting date string to date number.
Star Strider
Star Strider 2015-2-14
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
Peter Perkins 2017-2-9
编辑:Stephen23 2017-2-9
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

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by