How do I assign the same value to every field, including nested fields of a structure?
    3 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    John Petersen
      
 le 25 Fév 2016
  
    
    
    
    
    Réponse apportée : Walter Roberson
      
      
 le 26 Fév 2016
            Say I have a structure which has an unknown number of fields and each field may or may not have subfields. I want to assign a certain value to each field value. I would be okay only going two fields deep into the structure.
For example: Let A have fields a,b, and field a has fields c,d, but field b has no fields. I want to assign all fields to be x so that the result is
A.a.c = x;
A.a.d = x;
A.b = x;
0 commentaires
Réponse acceptée
  Walter Roberson
      
      
 le 26 Fév 2016
           A = structfun(@(s) layer2(s,x), 'Uniform', 0);
together with
   function r = layer2(s, x)
     if istruct(s)
       fn = fieldnames(s);
       r = cell2struct( repmat({x}, length(fn), 1), fn, 1);
     else
       r = x;
     end
  end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Structures dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
