How to dynamically create a struct?

조회 수: 69 (최근 30일)
Keith Lewis
Keith Lewis 2017년 2월 1일
댓글: Walter Roberson 2017년 2월 1일
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