Converting Struct field to array
조회 수: 1,325 (최근 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!