MATLAB: Table: Reshape based on Cell Column and Sort

columnreshapesorttable

Is there a way to reshape or transform a table 7×3 table into a table with as many columns as the unique elements of a cell column?
Orignal table 7×3 (var1 double, var2 cell, var3 double)
Table1=
1 AAA 100
3 AAA 500
1 BBB 300
2 AAA 250
4 BBB 50
2 BBB NaN
3 BBB 100
A sub table,3×3, for only AAA would be
Table2=
1 AAA 100
2 AAA 250
3 AAA 500
A subtable, 4×3, for only BBB would be
Table3=
1 BBB 300
2 BBB NaN
3 BBB 100
4 BBB 50
The target table, 4×3, is
Table4=
1 100 300
2 250 NaN
3 500 100
4 NaN 50
I do not need table 2 and 3, only interested in table 4. Table 4 keeps the first column and following columns refer to the AAA BBB …

Best Answer

unstack will do exactly that:
>> t = table([1;3;1;2;4;2;3], {'AAA';'AAA';'BBB';'AAA';'BBB';'BBB';'BBB'},[100;500;300;250;50;NaN;100])
t =
7×3 table
Var1 Var2 Var3
____ _____ ____
1 'AAA' 100
3 'AAA' 500
1 'BBB' 300
2 'AAA' 250
4 'BBB' 50
2 'BBB' NaN
3 'BBB' 100
>> unstack(t, 'Var3', 'Var2')
ans =
4×3 table
Var1 AAA BBB
____ ___ ___
1 100 300
3 500 100
2 250 NaN
4 NaN 50