MATLAB: Find n highest values in each row of a table

highestmaxrowsorttablevalue

Hi everyone,
I'm looking for a way to find the two highest values of a table and post them to a second table. Consider following example table:
T = table({'Jan';'Feb';'March';'Oct';'Dec'},[38;43;38;40;49],[71;69;64;67;64],[176;163;131;133;119],[1;55;5;80;110]);
Var1 Var2 Var3 Var4 Var5
_______ ____ ____ ____ ____
'Jan' 38 71 176 1
'Feb' 43 69 163 55
'March' 38 64 131 5
'Oct' 40 67 133 80
'Dec' 49 64 119 110
The outcome should be something like this:
'Jan' 71 176
'Feb' 69 163
'March' 64 131
'Oct' 67 133
'Dec' 119 110
I tried a lot of functions such as sort, sortrows, max etc. but nothing worked for my purpose.
Thank you in advance!

Best Answer

17b required for maxk but two lines with sort would also work:
[T.Var1, rowfun(@(varargin)maxk([varargin{:}],2,2),T(:,2:end))]
Or maybe more readable:
table(T.Var1, maxk(T{:,vartype('numeric')},2,2))