Nested struct arrays with variable format
조회 수: 2 (최근 30일)
이전 댓글 표시
What is the syntax for creating a nested struct array with the following format?
Varibale2_Name = struct('FieldName1',cell(Variable1_Name,1),'FieldName2',cell(Variable1_Name,1),'FieldName3',*struct*)
Where *struct* is meant to contain basically any other information/data returned from multiuple different functions called within a loop. I would like to be able to chuck a 1x7 struct, a 3x10 cell, etc. into that last field, essentailly treating it as a pointer/container. I am doing this specifically to avoid creating separate aggregator/container variables for each possible format of FieldName3 data, which I would have to concatenate with each iteration through a loop because, while Variable1_Name is known prior to the loop, the size of FieldName3 data cannot be known a-priori.
댓글 수: 0
채택된 답변
Jan
2021년 9월 10일
You can't.
S = struct('Name1', cell(n1, 1), ...
'Name2', cell(n1, 1))
his creates a [n1 x 1] struct array with two fields. You cannot append an aribtrary struct magically to S. Struct array need to have the same fields for all elements.
Either you need another level of substructs or you can store the data of as arrays:
S.Value = struct('Name1', cell(n1, 1), 'Name2', cell(n1, 1))
S.Name3 = yourStruct;
% or:
S.Name1 = cell(n1, 1)
S.Name2 = cell(n1, 1)
S.Name3 = yourStruct;
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!