Converting cell array to struct

3 次查看(过去 30 天)
Chris Dan
Chris Dan 2020-1-18
评论: Guillaume 2020-1-20
Hello,
I am trying to convert a cell array "zq" shown below:
pic1.JPG
into a struct, with this code:
fields_1 = {'individualStiffnessMatrix'}; % convert cell to struct
zq_1 =cell2struct(zq,fields_1,2);
but I am getting this error, does any one know about it
" Error using cell2struct
Number of field names must match number of fields in new structure."
I just want two fields in struct like a 2x1 struct not more
  2 个评论
Image Analyst
Image Analyst 2020-1-19
What fields do you want your structure to have? One field called "individualStiffnessMatrix" and the value of that field to be a 2-by-7 cell array, or a 2-by-7 double array, or something else?
Walter Roberson
Walter Roberson 2020-1-19
After the conversion it has to be the case that number of array elements times number of fields times number of values per field equals the number of original elements. You are asking for two array elements each with two fields, so the number of elements per field would have to be the number of original elements divided by four. But the number of original elements is 14 which does not divide by 4.
This can only be reconciled if the number of elements per field is not divided up evenly, such as if one field gets 2 elements and the other gets 5.
You need to make clear how many elements are to go into each field in order for us to recommend a conversion.

请先登录,再进行评论。

回答(1 个)

Robert U
Robert U 2020-1-18
Hi hamzah khan,
it is not clear to me how your output should look like. Documentation of cell2struct reports several examples. Interpreting your cell array may only have either two or seven fieldnames but not just one.
zq = {1 1 1 1 2 2 2 ;
1 1 1 2 2 2 2};
fields_1 = {'myFieldname_1','myFieldname_2'};
zq_1 =cell2struct(zq,fields_1,1);
Maybe the conversion you are looking for is not provided by cell2struct. If you could comment on what output you expect, it would be easier to answer thoroughly.
Kind regards,
Robert
  3 个评论
Robert U
Robert U 2020-1-20
Please, show how the structure should look like with the data of your example.
E.g.
1x1 struct with field 'individualStiffnessMatrix'
myStruct.individualStiffnessMatrix.zq
1x1 struct with fields 'myField1' and 'myField2'
myStruct.myField1.zq(1,:)
myStruct.myField2.zq(2,:)
2x1 struct with field 'individualStiffnessMatrix'
myStruct(1).individualStiffnessMatrix.zq(1,:)
myStruct(2).individualStiffnessMatrix.zq(2,:)
Guillaume
Guillaume 2020-1-20
"I am doing struct becasue they are easy to access and with loops we only need one "
I would argue that using a structure will actually make it harder to use not easier. The matrix that you now have is much simpler to access. The matrix also uses less memory.
In addition, since you now have a matrix, it's very much possible that you don't even need a loop to do whatever it is you want doing.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by