Converting Struct field to array
1,324 次查看(过去 30 天)
显示 更早的评论
I have an n-value structure array with multiple fields, and want to extract a specific field (all values thereof are (1x4) single arrays) into a nx4 array.
MyMatrix = [MyStruct(:).FieldC];
concatenates the values of the field, giving me a 1x(n*4) array. There's probably a way to reshape that, but I would appreicate a more direct solution as I'm doing this in the command window on an as-needed basis, rather than via a script.
0 个评论
采纳的回答
James Tursa
2021-6-9
Does this do what you want?
MyMatrix = vertcat(MyStruct.Field);
更多回答(1 个)
David Hill
2021-6-9
MyMatrix=cell2mat(struct2cell(MyStruct));
2 个评论
Matt Weber
2022-10-11
David Hill's solution worked for me, the other one did not. My context was a struct with just one field level.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!