How to dynamically create a struct?

60 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2017-2-1
a = struct ;
for i = 1:10
a(i).x = i ;
a(i).y = i+1 ;
end
It depends on how your structure is.
  3 个评论
KSSV
KSSV 2017-2-1
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 2017-2-1
Index a cell array or multidimensional array. x(intersectionnumber, visitnumber)

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-2-1
TheStructure = cell2struct( cell(1, length(NamesCell)), NamesCell, 2 )

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by