3

How do I unstack a data in matlab? The unstack for tables only allows me to do it for one column, and does not seem to be able to do a Multi Index solution as available in Python.

I was hoping to convert this:

Raw Table

into this:

enter image description here

in Matlab.

Here is code to produce the initial table T:

Col1 = [1,2,4,1,3,1,2,3,4].';
Col2 = {'A','A','A','B','B','C','C','C','C'}.';
Col3 = [0.48,0.78,0.58,0.01,0.53,0.26,0.93,0.69,0.45].';
Col4 = [0.95,0.29,0.90,0.72,0.07,0.23,0.81,0.22,0.88].';

T = table( Col1, Col2, Col3, Col4 );
0

1 Answer 1

3

You can do this with the unstack function by specifying more than one variable for the 2nd input

Col1 = [1,2,4,1,3,1,2,3,4].';
Col2 = {'A','A','A','B','B','C','C','C','C'}.';
Col3 = [0.48,0.78,0.58,0.01,0.53,0.26,0.93,0.69,0.45].';
Col4 = [0.95,0.29,0.90,0.72,0.07,0.23,0.81,0.22,0.88].';

T = table( Col1, Col2, Col3, Col4 );

tUnstack = unstack( T, {'Col3','Col4'}, 'Col2'  );

Output:

unstacked table

Note that, in MATLAB, you cannot have multiple header rows in a table. The unstack function concatenates header names (as shown above) for multiple categories, or can take an additional 'NewDataVariableNames' argument if you wish to specify them.

If you needed multiple header rows, you would have to parse out the headers and store the new data in a cell array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.