question about the 'stuct' of matlab?
조회 수: 2 (최근 30일)
이전 댓글 표시
Why the second one gives rise to empty struct?
s.a=1; s.b=2; s.c={}
s =
a: 1
b: 2
c: {}
s = struct('a',1,'b',2,'c',{})
s =
0x0 struct array with fields:
a
b
c
댓글 수: 0
채택된 답변
per isakson
2014년 11월 24일
편집: per isakson
2014년 11월 24일
That's the way the function struct is designed to work. See struct, Create structure array "[...]If value is an empty cell array {}, then s is an empty (0-by-0) structure."
댓글 수: 4
Guillaume
2014년 11월 24일
편집: Guillaume
2014년 11월 24일
If you do want to assign an empty cell array to a field of a structure using the struct function, you need to wrap that empty cell array into a cell array:
s = struct('a', 1, 'b', 2, 'c', {{}})
s =
a: 1
b: 2
c: {}
For that matter, even if the cell array is not empty you still need to wrap it to assign it to a scalar structure, otherwise the elements of the cell arrays are just distributed over an array of structs.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!