How to convert a table into a structure

Hi,
Lets say I have a table that looks like this:
Name = {'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'}.';
Month = [1, 2, 3, 1, 2, 3, 1, 2, 3].';
Lat = [49, 50, 51, 52, 53, 54, 49, 50, 51].';
Lon = [-99, -100, -101, -102, -103, -104, -99, -100, -101,].';
A = [0.1, 0.2 , 0.3, 1.1, 0.9, 1.0, 0.1, 0.2 , 0.3,].';
T = table(Name, Month, Lat, Lon, A);
How would I convert this table into a 1 x 3 structure with the following shape ?
Name Month Lat Lon A
A 1x3 double 1x3 double 1x3 double 1x3 double
B 1x3 double 1x3 double 1x3 double 1x3 double
C 1x3 double 1x3 double 1x3 double 1x3 double

1 件のコメント

Blue
Blue 2019 年 8 月 1 日
Neat! Thank you.

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

 採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 1 日

1 投票

t2 = varfun(@(x){x(:)'},T,'GroupingVariables','Name');
TinStrr = t2(:,[1,3:end]);
TinStrr.Properties.VariableNames = T.Properties.VariableNames;
S_out = table2struct(TinStrr);

その他の回答 (1 件)

KSSV
KSSV 2019 年 8 月 1 日

0 投票

Read about table2struct.
S = table2struct(T)

3 件のコメント

madhan ravi
madhan ravi 2019 年 8 月 1 日
Note it produces a 9 X 1 strict array not 1 X 3 array.
KSSV
KSSV 2019 年 8 月 1 日
idx = strcmp(T.Name,'A') ;
S = table2struct(T(idx,:))
Blue
Blue 2019 年 8 月 1 日
Thank you for your input but I am trying to do something more complicated. More akin to a nested structure I guess where I would have 1 structure containing 3 structures (1 for each name as outlined above)

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

カテゴリ

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

タグ

質問済み:

2019 年 7 月 31 日

コメント済み:

2019 年 8 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by