Cell Array to Structures
9 次查看(过去 30 天)
显示 更早的评论
Is there a way to take a 1xM cell array and transform that into a structure array without using the function struct()?
The cell array will always be formatted this way: CellArr = {<field1 name>, {cell array of field 1 contents}, <field2 name, {cell array of field 2 contents}, etc...}
0 个评论
采纳的回答
Walter Roberson
2015-10-18
Note: this will use struct() internally.
2 个评论
Walter Roberson
2015-10-19
You could use dynamic field assignment.
YourStruct = [];
for K = 1 : 2: length(CellArr)
YourStruct.(CellArr{K}) = CellArr{K+1};
end
It would be stylistically better to initialize
YourStruct = struct();
but you said you wanted to not use struct().
By the way, are you aware that you can do the whole thing by using
YourStruct = struct(CellArr{:});
? Though you would have to watch out for cases where the contents included cell arrays as struct() would tend to create a structure array for those cases instead of a single struct.
更多回答(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!