フィルターのクリア

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

54 ビュー (過去 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 件のコメント
Eric Sampson
Eric Sampson 2013 年 1 月 18 日
Brilliant! I couldn't figure out how to make the fieldnames and values alternate, but your solution is the key :) Thanks Matt!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by