MATLAB: Creating an array of structures

structures

Hey Community,
I am trying to solve a problem where I want to assign a field within a structure to another structure that contains a cell array of structures. For example, lets say I have some structure, pet.dog.type that I want to use to create another structure. For example dog.name1 = pet.dog.type{1}.name, dog.name2 = pet.dog.type{2}.name. Is there a way to create a structure in this way? When I try to do this I keep getting an error "unable to perform assignment because dot indexing is not supported for variables of this type."

Best Answer

When you say "pet.dog.type{1}.name," you're saying that one of the fields, "grand field" if you'd like rather than a child field, is actually a cell array. So if you did
ca = pet.dog.type{1};
you'd have a cell. ca would be a cell, NOT another structure. Cells cannot take fields. So you cannot do
v = ca.name;
because ca is a cell array and cell arrays do not take dot indexing - they have no field so they can't, it's impossible. So that's why you cannot say pet.dog.type{1}.name.