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!