question about the 'stuct' of matlab?
1 次查看(过去 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!