How to create a struct from a cell array of fieldnames and a cell array of values?

84 次查看(过去 30 天)
How can you take a cell array array of fieldnames and a cell array of values, and convert them into a structure without looping?
names = {'f1', 'f2'};
values = {1 2};
structure = struct(???)

采纳的回答

Matt J
Matt J 2013-1-18
编辑:Matt J 2013-1-18
args=[names;values];
structure = struct(args{:})

更多回答(1 个)

Matt J
Matt J 2013-1-18
cell2struct(values,names,2)
  2 个评论
Eric Sampson
Eric Sampson 2013-1-18
I always find the cell2struct syntax tricky. I figured that it should be possible, but gave up. Thanks!
Jan
Jan 2013-1-18
It is not documented but even slightly faster, to omit the 3rd input dimension, when values and names are {nx1} cells.

请先登录,再进行评论。

类别

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