Nested struct arrays with variable format

조회 수: 2(최근 30일)
Gabriel Stanley
Gabriel Stanley 2021년 9월 10일
댓글: Jan 2021년 9월 11일
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.

채택된 답변

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
Jan 2021년 9월 11일
If it works, it works.

댓글을 달려면 로그인하십시오.

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by