MATLAB: How to separate table columns by groups

tableunstack

If I have a table of data (T1) such as:
Var1 Var2
1 xyz
1 xyy
2 xxx
2 xzx
3 xyy
4 yzy
4 xzz
4 yzz
I would like to output another table (T2):
Var1 Var2
1 xyz,xyy
2 xxx,xzx
3 xyy
4 yzy,xzz,yzz
I have unsuccessfully used unstack, since it creates the values in Var2 as column names. Any suggestions would be much appreciated.

Best Answer

If Var2 is a categorical array, then I assume that your xyz,xyy is actually a 1x2 categorical array [xyz, xyy], not a char array. If so:
T1 = table([1;1;2;2;3;4;4;4], categorical({'xyz';'xyy';'xxx';'xzx';'xyy';'yzy';'xzz';'yzz'}))
varfun(@(values) {values'}, T1, 'GroupingVariables', 'Var1')