Table won't sort?

16 views (last 30 days)
Rachel McLaughlin
Rachel McLaughlin on 4 May 2016
Commented: Guillaume on 5 May 2016
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 Comment
Stephen23
Stephen23 on 4 May 2016
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:

Sign in to comment.

Answers (3)

Guillaume
Guillaume on 4 May 2016
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 Comments
Guillaume
Guillaume on 4 May 2016
Well then, it should be
Day = sortrows(Day, {'Year' ,'Month', 'Day'})
The order of the variable matters for sortrows.

Sign in to comment.


Star Strider
Star Strider on 4 May 2016
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 on 4 May 2016
Edited: J. Webster on 4 May 2016
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 Comments
Guillaume
Guillaume on 5 May 2016
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!