How to dynamically create a struct?

69 vues (au cours des 30 derniers jours)
Keith Lewis
Keith Lewis le 1 Fév 2017
Commenté : Walter Roberson le 1 Fév 2017
I want to create a struct with the number of fields decided based on some user input. What's a good way of doing this? Or should I be using a cell or matrix instead?
Thanks

Réponse acceptée

KSSV
KSSV le 1 Fév 2017
a = struct ;
for i = 1:10
a(i).x = i ;
a(i).y = i+1 ;
end
It depends on how your structure is.
  3 commentaires
KSSV
KSSV le 1 Fév 2017
a = struct ;
for i = 1:10
for j = 1:10
a(i).x(j) = rand ;
a(i).y(j) = rand+1 ;
end
end
Walter Roberson
Walter Roberson le 1 Fév 2017
Index a cell array or multidimensional array. x(intersectionnumber, visitnumber)

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 1 Fév 2017
TheStructure = cell2struct( cell(1, length(NamesCell)), NamesCell, 2 )

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