Table won't sort?

2 次查看(过去 30 天)
Rachel McLaughlin
评论: Guillaume 2016-5-5
Hello all,
I have a table that is the product of this function: Day =varfun(@sum,Table,'InputVariables','Rain_mm','GroupingVariables',{'Month' 'Day' 'Year'});
The output is a table where the date is split into three columns (Day, Month, Year) and one column is the summed rain per day (sum_Rain_mm)
The problem I am having is that the output table 'Day' looks like this:
Day Month Year GroupCount sum_Rain_mm
___ _____ ____ __________ ___________
1_1_2016 1 1 2016 48 NaN
1_7_2015 1 7 2015 48 0
1_8_2015 1 8 2015 48 0
1_9_2015 1 9 2015 48 0.2
1_10_2015 1 10 2015 48 NaN
1_11_2015 1 11 2015 48 NaN
1_12_2015 1 12 2015 48 NaN
2_1_2016 2 1 2016 48 NaN
2_7_2015 2 7 2015 48 0
2_8_2015 2 8 2015 48 0
2_9_2015 2 9 2015 48 0
2_10_2015 2 10 2015 48 NaN
2_11_2015 2 11 2015 48 NaN
2_12_2015 2 12 2015 48 NaN
3_1_2016 3 1 2016 48 NaN
3_7_2015 3 7 2015 48 0
So the datestamps and totaled rain values are not in chronological order. When I try to make a plot of the rain data then against the time span (in days), the values are all out of order.
I cannot seem to get this output table to sort such that the data is chronological. I have tried:
Day = sortrows(Day, {'Day' ,'Month', 'Year'})
But it doesn't work. Any suggestions?
Thanks
  1 个评论
Stephen23
Stephen23 2016-5-4
编辑:Stephen23 2016-5-4
The simplest solution is to use ISO 8601 date format timestamps, because all ISO 8601 dates sort into chronological order without any extra work, just a standard sort:
yyyy-mm-dd
No splitting, converting to numeric, sorting of columns, or any mucking around with date functions is required to get the correct chronological order. Simply pick the only reliable date format in the world and your problem would be resolved:

请先登录,再进行评论。

回答(3 个)

Guillaume
Guillaume 2016-5-4
1) The columns of your table appear to be mislabeled since the Year appear to be months (or is it days), the Month column appears to be days and the Year column appears to be everything. That's down to the way you created the original table in the first place.
2) The name of the table is not very good. DailyRain may be better
3) What is the type of the Year column? To find out see the output of
summary(Day)
Is it a cell array of strings, or a datetime array, or something else?
  2 个评论
Rachel McLaughlin
Sorry, the alignment of the table headings was off when I copied it over. The Day/Month/Year headings do match up to the correct columns. The Year is a 213 x 1 double, as are the Day and Month.
Guillaume
Guillaume 2016-5-4
Well then, it should be
Day = sortrows(Day, {'Year' ,'Month', 'Day'})
The order of the variable matters for sortrows.

请先登录,再进行评论。


Star Strider
Star Strider 2016-5-4
This is a bit less efficient than I’d like, but it has the virtue of working. I’m including the table creation as well so others won’t have to re-create that part of the code:
T = {'Day' 'Month' 'Year' 'Group' 'Count' 'sum_Rain_mm'
'1_1_2016' 1 1 2016 48 NaN
'1_7_2015' 1 7 2015 48 0
'1_8_2015' 1 8 2015 48 0
'1_9_2015' 1 9 2015 48 0.2
'1_10_2015' 1 10 2015 48 NaN
'1_11_2015' 1 11 2015 48 NaN
'1_12_2015' 1 12 2015 48 NaN
'2_1_2016' 2 1 2016 48 NaN
'2_7_2015' 2 7 2015 48 0
'2_8_2015' 2 8 2015 48 0
'2_9_2015' 2 9 2015 48 0
'2_10_2015' 2 10 2015 48 NaN
'2_11_2015' 2 11 2015 48 NaN
'2_12_2015' 2 12 2015 48 NaN
'3_1_2016' 3 1 2016 48 NaN
'3_7_2015' 3 7 2015 48 0};
Tbl1 = cell2table(T(2:end,:));
Tbl1.Properties.VariableNames = T(1,:);
Tbl_Dates = Tbl1.Day;
new_dates = regexp(Tbl_Dates, '\d*','match');
Dmtxc = cellfun(@(x)sprintf('%02s%02s%4s',x{:}), new_dates, 'Uni',0);
Tbl_DN = datenum(Dmtxc, 'ddmmyyyy');
[~,idx] = sort(Tbl_DN);
Tbl2 = Tbl1(idx,:)
Tbl2 =
Day Month Year Group Count sum_Rain_mm
___________ _____ ____ _____ _____ ___________
'1_7_2015' 1 7 2015 48 0
'2_7_2015' 2 7 2015 48 0
'3_7_2015' 3 7 2015 48 0
'1_8_2015' 1 8 2015 48 0
'2_8_2015' 2 8 2015 48 0
'1_9_2015' 1 9 2015 48 0.2
'2_9_2015' 2 9 2015 48 0
'1_10_2015' 1 10 2015 48 NaN
'2_10_2015' 2 10 2015 48 NaN
'1_11_2015' 1 11 2015 48 NaN
'2_11_2015' 2 11 2015 48 NaN
'1_12_2015' 1 12 2015 48 NaN
'2_12_2015' 2 12 2015 48 NaN
'1_1_2016' 1 1 2016 48 NaN
'2_1_2016' 2 1 2016 48 NaN
'3_1_2016' 3 1 2016 48 NaN

J. Webster
J. Webster 2016-5-4
编辑:J. Webster 2016-5-4
One way to do this is to add another column in your table made up of datenums generated by your dates. Then sort your table based on that column.
myDatenums = datenum(Day{:,1}) %generate datenums from your first column
myNewTable = table(myDatenums); %make myDatenums into a table so you can concatenate it.
Day = [Day myNewTable] %append new column
Day = sortrows(Day,7); %sort table based on new column
Day(:,7) = []; %delete the new column
I learned a while ago to save dates as yyyymmdd to avoid problems like this.
  3 个评论
J. Webster
J. Webster 2016-5-4
whatever
Guillaume
Guillaume 2016-5-5
It's not 'whatever'. If you try to do anything complex with dates, you'll quickly run into issues with datenum that you won't have with datetime.
If you're giving advice, give advice that leads to good practice.

请先登录,再进行评论。

类别

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