How to convert cell array to structure array

I would like to convert cell array to a structure 1x2 (or 2x1) with 3 fields with a1 as dimension/axis:
a1={'AA','AA','AA','BB','BB','BB','BB'}';
a2={'35000','96000','88000','33000','44000','8000','18000'}';
a3={'1a','1b','1c','1d','1e','1a','1b'}';
a123=[a1,a2,a3];
rowHeadings = {'first', 'second', 'third'};
but this does not work:
depts = cell2struct(a123, rowHeadings, 1)
Error using cell2struct
Unknown command option.

7 件のコメント

Geoff Hayes
Geoff Hayes 2015 年 6 月 13 日
Aleksander - which version of MATLAB are you using? If I try the above, on R2014a, and change the depts initialization to
depts = cell2struct(a123', rowHeadings, 1)
then it works fine, with
depts =
7x1 struct array with fields:
first
second
third
Note that the transpose of a123 is necessary since rowheadings is a 1x3 cell array, and so the first input to cell2struct must be a 3x7.
Aleksandar
Aleksandar 2015 年 6 月 14 日
Geoff thank you - it works. However now I have 7x1 struct with 3 fields. But I should have 1x2 structure (one for AA and second for BB)as they are distinct categories with 3 fields (which is ok). That is to say I want to group AAs and BBs in 2 categories.
Aleksandar
Aleksandar 2015 年 6 月 14 日
...a2 and a3 should be vectors instead.
Geoff Hayes
Geoff Hayes 2015 年 6 月 15 日
Aleksander - please provide an example of how you would like the structure to appear. Also, what do you mean by a2 and a3 should be vectors instead? Aren't they already arrays?
Aleksandar
Aleksandar 2015 年 6 月 15 日
I would like structure to appear like that: First Second Third AA [35000 96000 88000] [1a 1b 1c] BB [33000 44000 8000 18000] [1d 1e 1a 1b] The grouping categories is data in rowheadings first, i.e.: 'AA' and 'BB' (as if in idTotals in Credit risk utitilites)
Stephen23
Stephen23 2015 年 6 月 15 日
編集済み: Stephen23 2015 年 6 月 15 日
@Aleksandar: it looks like you are a bit confused about what structures can do. Structures are not really categorization tools, nor are they dictionaries (which might look similar to what you wrote above). Like most traditional MATLAB data classes the dimensions of a structure do not encode any meta-data, so you cannot "name" the dimensions of a 1x2 structure 'AA' and 'BB', atleast not as meta-data in the structure itself. It would be possible to either:
  • create a separate array containing this data, or
  • include this meta-data as data withing the structure (see my answer below).
  • use another data class: it seems like you might really be looking for something more like a table. Have you looked at using one?
Vahab Youssofzadeh
Vahab Youssofzadeh 2016 年 7 月 22 日
Try the following:
table2struct(cell2table(.))

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 13 日

2 投票

a1={'AA','AA','AA','BB','BB','BB','BB'}';
a2={'35000','96000','88000','33000','44000','8000','18000'}';
a3={'1a','1b','1c','1d','1e','1a','1b'}';
a123=[a1,a2,a3];
rowHeadings = {'first', 'second', 'third'};
v=cell2struct(a123,rowHeadings,2)

3 件のコメント

Aleksandar
Aleksandar 2015 年 6 月 14 日
Hi Azzi, is there a possibility to convert it to 1x2 structure with 3 fields (AA i BB groups)?
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 15 日
a1={'AA','AA','AA','BB','BB','BB','BB'}';
a2={'35000','96000','88000','33000','44000','8000','18000'}';
a3={'1a','1b','1c','1d','1e','1a','1b'}';
a123=[a1,a2,a3];
rowHeadings = {'first', 'second', 'third'};
ii=unique(a1)
for k=1:numel(ii)
idx=ismember(a1,ii{k})
v(k).first=ii{k}
v(k).second=a2(idx)
v(k).third=a3(idx)
end
Stephen23
Stephen23 2015 年 6 月 15 日
Note that it is poor practice in MATLAB to expand arrays inside loops, even for structures and cell arrays:
It is better either to create the entire structure in one call (see my answer), or to preallocate the structure (e.g. looping in reverse).

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

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 6 月 15 日
編集済み: Stephen23 2015 年 6 月 15 日

0 投票

Here is a short and reasonably tidy way of doing this using struct:
a1 = {'AA','AA','AA','BB','BB','BB','BB'}';
a2 = {'35000','96000','88000','33000','44000','8000','18000'}';
a3 = {'1a','1b','1c','1d','1e','1a','1b'}';
hdr = {'first', 'second', 'third'};
fun = @(x)arrayfun(@(s)x(strcmp(s,a1)), unique(a1,'stable'), 'UniformOutput',false);
X = struct(hdr{1},fun(a1), hdr{2},fun(a2), hdr{3},fun(a3));
Now lets check it in the command window:
>> X
X =
2x1 struct array with fields:
first
second
third
>> X.first
ans =
'AA'
'AA'
'AA'
ans =
'BB'
'BB'
'BB'
'BB'
>> X.second
ans =
'35000'
'96000'
'88000'
ans =
'33000'
'44000'
'8000'
'18000'

2 件のコメント

Aleksandar
Aleksandar 2015 年 6 月 15 日
Thank you! I accepted the answer(s) , I suppose it refers to both answers.
Stephen23
Stephen23 2016 年 7 月 23 日
"I suppose it refers to both answers"
No, it doesn't. You can vote for other answers though.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by