Effacer les filtres
Effacer les filtres

How to display structs only in a struct with fields?

2 vues (au cours des 30 derniers jours)
Emily
Emily le 20 Avr 2022
Commenté : Emily le 20 Avr 2022
I'm trying to display only the 1x1 struct portions and hide the character portions in a struct with fields.
aTable=struct2table(S);
disp(aTable);
  2 commentaires
Rik
Rik le 20 Avr 2022
Do you have an example of what kind of output you want? Can you provide a small example struct with the intended output?
Emily
Emily le 20 Avr 2022
I want to show the output as the fieldnames with struct only and hide the chars.
So only show 'b' and 'c'.
s =
struct with fields:
a: [2×4 char]
b: [1×1 struct]
c: [1×1 struct]
d: [2×4 char]

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 20 Avr 2022
Modifié(e) : Matt J le 20 Avr 2022
t='11'; s.q=1;
S=struct('a',t,'b',s,'c',s,'d',t)
S = struct with fields:
a: '11' b: [1×1 struct] c: [1×1 struct] d: '11'
aTable=struct2table(S);
disp( table2struct(aTable(:,vartype('struct'))) )
b: [1×1 struct] c: [1×1 struct]
  1 commentaire
Emily
Emily le 20 Avr 2022
The last line solved it!
Thanks for the help.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 20 Avr 2022
Modifié(e) : Matt J le 20 Avr 2022
t='11'; s.q=1;
S=struct('a',t,'b',s,'c',s,'d',[s,s])
S = struct with fields:
a: '11' b: [1×1 struct] c: [1×1 struct] d: [1×2 struct]
p=reshape( namedargs2cell(S) , 2,[]);
keep=structfun(@(f)isstruct(f) & isscalar(f), S);
s=struct(p{:,keep});
disp(s)
b: [1×1 struct] c: [1×1 struct]

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by