Transpose a structure with named fields ?

67 vues (au cours des 30 derniers jours)
Flynn McGuire
Flynn McGuire le 24 Juin 2020
Commenté : Flynn McGuire le 24 Juin 2020
I have a struct that looks like this:
i.g. labeled fields
I have been trying to apply this example:
% Fake data
s(1).f = rand(1,100);
s(2).f = rand(1,100);
s(3).f = rand(1,100);
c = arrayfun(@(x)x.f.', s, 'UniformOutput', 0);
s = cell2struct(c, 'f');
but I think it doesn't work becuase I have labeled feilds.
Any ideas?
  1 commentaire
Stephen23
Stephen23 le 24 Juin 2020
Modifié(e) : Stephen23 le 24 Juin 2020
"Any ideas?"
Your screenshot shows a scalar structure with multiple fields.
Your example code has a non-scalar structure with one field.
It is not clear how that example would apply to the scalar structure shown in your screenshot.
You could use structfun like Mehmed Saad showed, or simply loop over the fieldnames:

Connectez-vous pour commenter.

Réponse acceptée

Mehmed Saad
Mehmed Saad le 24 Juin 2020
if all of them are scalar then this will work
s_mod=structfun(@transpose,s,'UniformOutput',false)
  1 commentaire
Flynn McGuire
Flynn McGuire le 24 Juin 2020
Perfect thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by