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
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
Gabriel Stanley
Gabriel Stanley 2021년 9월 10일
So I think I did what you were talking about with the substructs thing without meaning to. I ran individual segments of the loop in the cmd window to get data sets, but with manual assignment was able to create the following:
AggregateData = struct('ConsistentInfo',cell(n1,1),'Semi-ConsistentInfo',cell(n1,1),'WildcardHolder',struct)
Fields ConsistentInfo Semi-ConsistentInfo WildcardHolder
1 1x5 cell [n2 x 2] double [1x16] struct
2 1x5 cell [n3 x 2] double [10x3] cell
3 1x5 cell [n4 x 2] double [1x7] struct
... ... ... ...
n1 1x5 cell [n{n1} x 2] double [1x1] struct
Which is what I was trying to do. Are there any potential consequences of what I've done here I should be warry of?
Jan
Jan 2021년 9월 11일
If it works, it works.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by