how to set multiple fields in a struct without using a loop

3 vues (au cours des 30 derniers jours)
Lisandro Jimenez
Lisandro Jimenez le 6 Juin 2014
Hello guys, I have the next problem, I want to make something like this:
A={'A1';'A2';'A3'};
them make B=somefunction(B,A); and obtain this:
B =
A1: []
A2: []
A3: []
Any idea?
  4 commentaires
José-Luis
José-Luis le 6 Juin 2014
B is a struct with fields A1, A2 and A3?
Lisandro Jimenez
Lisandro Jimenez le 6 Juin 2014
That's right !

Connectez-vous pour commenter.

Réponse acceptée

José-Luis
José-Luis le 6 Juin 2014
Modifié(e) : José-Luis le 6 Juin 2014
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},3,1),A);
Please accept an answer if it helped you.
  1 commentaire
Lisandro Jimenez
Lisandro Jimenez le 6 Juin 2014
It's better:
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},size(A,1),1),A);

Connectez-vous pour commenter.

Plus de réponses (1)

Lisandro Jimenez
Lisandro Jimenez le 6 Juin 2014
for i=1:size(A,1)
B.(A{i})=[];
end
But this answer has a loop, and I don't know if there's another way more efficent.

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