Converting cell array to struct
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I am trying to convert a cell array "zq" shown below:
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 Commenti
Image Analyst
il 19 Gen 2020
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
il 19 Gen 2020
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.
Risposte (1)
Robert U
il 18 Gen 2020
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 Commenti
Robert U
il 20 Gen 2020
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
il 20 Gen 2020
"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.
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!