How do I make a script to show the field names of struct in a struct
    9 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Emily
 el 21 de Abr. de 2022
  
    
    
    
    
    Comentada: Matt J
      
      
 el 22 de Abr. de 2022
            I currently have the code below to list out only the structs. 
sTable=struct2table(s); 
sStruct=sTable(:,vartype('struct'));
Inside the table there are 3 1x1 structs that I wanted to access. 
I can get there via clicking on the variables workspace or using individual fieldnames 
a=fieldnames(sStruct.v1_abc) 
but I wanted to have a script to automatically access it by entering the first colnmn names show the fields. 
Thanks in advance. 
2 comentarios
  Matt J
      
      
 el 21 de Abr. de 2022
				Please attach sStruct and also clarify "by entering the first colnmn names show the fields". 
Respuesta aceptada
  Matt J
      
      
 el 21 de Abr. de 2022
        Something  like this?
a1='abcd';
a2=[1 2 3]; 
s.a=char(a1,a2);
s.b.a = ones(3);
s.b.b = eye(4);
s.c.c = magic(5);
s.d=char(a2,a1);
showsubfields(s,'b','c')
function showsubfields(S,varargin)
   for i=1:numel(varargin)
     fn=varargin{i};
     if ~isstruct(S.(fn)); continue; end
     disp("Field: "+fn)
     subfns=fieldnames(S.(fn));
     disp(char("   "+subfns))
   end
end
6 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Structures en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

