Nested struct arrays with variable format

3 vues (au cours des 30 derniers jours)
Gabriel Stanley
Gabriel Stanley le 10 Sep 2021
Commenté : Jan le 11 Sep 2021
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.

Réponse acceptée

Jan
Jan le 10 Sep 2021
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 commentaires
Gabriel Stanley
Gabriel Stanley le 10 Sep 2021
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 le 11 Sep 2021
If it works, it works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by