How do I unstack/transpose table in Matlab?

I need to unstack/transpose a table but couldn''t do using unstack ot transpose functions. Please see attached file.

2 件のコメント

dpb
dpb 2019 年 7 月 31 日
So show us what you tried and what happened...showing an expected output would help I have no idea what you would want that to be.
MIKHAIL DEREVYANKO
MIKHAIL DEREVYANKO 2019 年 7 月 31 日
sorry for complicating things. the real question is as follows
if i have a table of non-numerical variables
id={'id1';'id1';'id2';'id2';'id2'};
cn={'c1';'c2';'c1';'c2';'c3'};
t=table(id,cn);
%unstack/transpose
t1=unstack(t,'cn','id');
the function unstack returns the following error:
Error using tabular/unstack (line 307)
You must specify AGGREGATIONFUNCTION for multiple rows in non-numeric data.
i have no idea what to input for aggregation function.
the desired output should be like this:
id={'id1';'id2'};
cn={'c1','c2',NaN;'c1','c2','c3'};
t1=table(id,cn);
t1=splitvars(t1);
any ideas what should i change/how to achieve the task?

サインインしてコメントする。

 採用された回答

TADA
TADA 2019 年 7 月 31 日

1 投票

unstack groups using a third column, but you can trick it to do what you want by adding a fake third column
id={'id1';'id1';'id2';'id2';'id2'};
cn={'c1';'c2';'c1';'c2';'c3'};
t=table(id,cn,cn,'VariableNames', {'id', 'cn', 'cn_'});
%unstack/transpose
t1=unstack(t,'cn_','cn');
t1 =
2×4 table
id c1 c2 c3
_____ ____ ____ ____
'id1' 'c1' 'c2' ''
'id2' 'c1' 'c2' 'c3'

4 件のコメント

MIKHAIL DEREVYANKO
MIKHAIL DEREVYANKO 2019 年 8 月 1 日
Thanks a lot. all works. It's very frustrating that Matlab documentation has nothing on it.
Guillaume
Guillaume 2019 年 8 月 1 日
That would be because unstack is not designed at all for this use case.
To be honest, I don't see the point of it. The only thing it may help with is visualising the data. Processing it afterward is going to be a lot more complicated than processing what you started with.
TADA
TADA 2019 年 8 月 1 日
編集済み: TADA 2019 年 8 月 1 日
I don't know what the OPs use case really is, but sometimes a good visualization is all the processing you need.
I have to agree that in this case theres no reason to blame the documentation, we are tricking unstack to do something it wasn't exatly intended for
stone_ward
stone_ward 2020 年 2 月 21 日
How were you able to do this without defining an aggregation function? I've been trying to use unstack on my table of non-numeric data, which is similar to the example, except that I do happen to have 3 columns of data. Everytime I try to use it, I get an error about needing and aggregation function. When I supply one, I get the error "AGGREGATIONFUNCTION must return a value whose size conforms to elements of hte data variable 'Data'."
Note: Data is my datavar.

サインインしてコメントする。

その他の回答 (1 件)

MIKHAIL DEREVYANKO
MIKHAIL DEREVYANKO 2019 年 8 月 1 日

0 投票

it's nothing to do with the visualisation. I need to pick up correct curves names in a batch validation exercise. it can be done many ways but having a file with aligned discounting curve names for all contracts helps a lot.

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by