How to convert a cell array of structures to array of structures

17 ビュー (過去 30 日間)
ysz
ysz 2016 年 12 月 21 日
コメント済み: ysz 2016 年 12 月 22 日
searchNode's nodeinfo is a cell array of structures.
nodeinfo = searchNode(neo4jconn, 'Comment')
nodeinfo.NodeData contains node data. It has a property key 'text'
>> nodeinfo.NodeData{1}.text
ans =
blahblah
Array of structures would be easier to work with and accesses can be vectorized
Please suggest a better than for loop:
s = size(nodeinfo.NodeData, 1);
for i = 1:s
comments(i).text = nodeinfo.NodeData{i}.text;
end
For example to collect text of all comments is now one-liner
text = [comments.text]
How can a cell array of structures be vectorized like that?

採用された回答

Guillaume
Guillaume 2016 年 12 月 21 日
As long as the structures in the cells have all the same fields and that these structures in the cells are either scalar or arrays with the same number of rows:
nodedata = [nodeinfo.NodeData{:}];
%or
nodedata = cell2mat(nodeinfo.NodeData);

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 12 月 21 日
Will cell2struct() for for you?
  1 件のコメント
ysz
ysz 2016 年 12 月 21 日
編集済み: ysz 2016 年 12 月 21 日
cell2struct(nodeinfo.NodeData, {'text'}, 2) will return s x1 struct array with field text
whereas for loop creates 1x s array of structures

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by