From: Roy on
The idea is to create a structure that has fields that can also be structures.
It's not clear from the manual whether this can be done since in the
struct documentation they specifically state that the field values can either
be scalars or cells but I have done it in the past so it seems to be allowable.
The problem is when I try to do a formal initialization of the struct with
structs as fields it doesn't work.

The idea was to create the following
Values(k).Name (Values has 3 fields Name, a struct called Lot, and a struct called StatSum)
Values(k).StatSum (StatSum in turn would have two fields Mean and Std)
.StatSum.Mean
.StatSum.Std
Values(k).Lot(i).Name (Lot would be a struct with 3 fields - Name, StatSum, and Bio)
.Lot(i).StatSum
.StatSum.Mean
.StatSum.Std
.Lot(i).Bio (Bio would be struct with 4 fields: Name, StatSum, TechReps, data)
.Bio(j).Name
.Bio(j).StatSum.
.Bio(j).StatSum.Mean
.Bio(j).StatSum.Std
.Bio(j).TechReps
.Bio(j).data

I did the following as an initialization but it doesn't seem to work and
I can't see why.

>> StatSum = struct('Mean',[0],'Std',[0]);
bio = struct('Name',{},'StatSum',StatSum,'TechReps',{},'data',[]);
Lot = struct('Name',{},'StatSum',StatSum,'Bio',bio);
Values = struct('Name',{},'StatSum',StatSum,'Lot',Lot)

Values =

0x0 struct array with fields:
Name
StatSum
Lot

>> Values.Lot %(nothing for output not even the field names)
>> Values.Lot.Bio
??? Dot name reference on non-scalar structure.

>> Values.Lot(1).Bio
>> Values.Lot(1).Bio(1)

>> Lot

Lot =

0x0 struct array with fields:
Name
StatSum
Bio

>> bio

bio =

0x0 struct array with fields:
Name
StatSum
TechReps
data

>> StatSum

StatSum =

Mean: 0
Std: 0

>> bio.StatSum
>>


The smaller structs seem to be there but they aren't being transferred over.
Any ideas as to what I'm doing wrong? I thought it might be that I have
empty matrices so that is why I initialized StatSum but it still isn't doing anything.

RK
From: Walter Roberson on
Roy wrote:

>>> StatSum = struct('Mean',[0],'Std',[0]);
> bio = struct('Name',{},'StatSum',StatSum,'TechReps',{},'data',[]);
> Lot = struct('Name',{},'StatSum',StatSum,'Bio',bio);
> Values = struct('Name',{},'StatSum',StatSum,'Lot',Lot)
>
> Values =
> 0x0 struct array with fields:
> Name
> StatSum
> Lot

When you use {} as an initializer for struct(), it indicates that you
are initializing a structure of 0 elements. If you want to initialize
each element to the empty cell, use {{}} . If you want to initialize
each name to the empty string, use '' .


>> StatSum = struct('Mean',[0],'Std',[0]);
>> StatSum
StatSum =
Mean: 0
Std: 0
>> bio = struct('Name',{{}},'StatSum',StatSum,'TechReps',{{}},'data',[]);
>> bio
bio =
Name: {}
StatSum: [1x1 struct]
TechReps: {}
data: []
>> Lot = struct('Name',{{}},'StatSum',StatSum,'Bio',bio);
>> Lot
Lot =
Name: {}
StatSum: [1x1 struct]
Bio: [1x1 struct]
>> Values = struct('Name',{{}},'StatSum',StatSum,'Lot',Lot)
Values =
Name: {}
StatSum: [1x1 struct]
Lot: [1x1 struct]