Dynamic fieldnames
Afficher commentaires plus anciens
hey all,
Im trying to create a new structure from an already existing structure using dynamic fieldnames - no joy however! I also tried using eval but I cant seem to get the syntax right.
struct1 has many sub structs but lets just assume that im trying to create a new structure using the character values from one of the fields of the old structure.
names = fieldnames(struct1.names)
field = fieldnames(struct1.field )
names = 'danny' 'edgar' 'larry'
field = 'one' 'two' 'three'
for i =1:length(names)
create = ('Newstruct.' names(i) . 'substruct' field(i))
eval (create )
end
in other words how do I use the character values from an already existing field as fieldnames in a loop?
I am able to use dynamic fieldnames and the eval statement to change the numeric parts of fieldnames but I cant seem to get them to work with all character fieldnames.
regards.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 27 Avr 2011
The closest I can seem to get to your existing code is that you seem want to execute:
Newstruct.(names{i}).substruct.(field{i})
which isn't going to do you much good as you that structure with those fields will not exist until you assign values there. Perhaps
Newstruct.(names{i}).substruct.(field{i}) = [];
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!