How to sort by date
조회 수: 37 (최근 30일)
이전 댓글 표시
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
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
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!