array of structures to cell array via comma-separated list

5 ビュー (過去 30 日間)
Eric Sampson
Eric Sampson 2013 年 1 月 28 日
Suppose I have the following array of structs:
data(1).f1 = 1;
data(2).f1 = 2;
data(1).f2 = 'foo';
data(2).f2 = 'bar';
And I want to put each struct into one cell of a two-element cell array:
struct_as_cell = arrayfun(@(x) x, data, 'UniformOutput', false);
Is there any easier syntax to do this? I would have hoped that data(:) would generate a comma-separated list of structs so that I could do the following, just as I could if data was a cell array, but it doesn't seem to work:
struct_as_cell = {data(:)};
Cheers
  4 件のコメント
Eric Sampson
Eric Sampson 2013 年 1 月 28 日
Cedric, the purpose is in order to use struct_as_cell as the valueSet for constructing a containers.Map . Unfortunately it seems that you cannot use arrays of structs directly as containers.Map valueSets, you have to convert them to this form (cell arrays of scalar structs).
Azzi, I was just hoping that there might be a simpler syntax to do this instead of using arrayfun :)
Cedric
Cedric 2013 年 1 月 28 日
Ah! I just learned something from Azzi's answer with num2cell().

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 28 日
編集済み: Azzi Abdelmalek 2013 年 1 月 28 日
for k=1:size(data,2)
out{k}=data(k)
end
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 28 日
%or
num2cell(data)
Eric Sampson
Eric Sampson 2013 年 1 月 28 日
Very good Azzi, I didn't even think of num2cell because I've only used it for numeric arrays! :)

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

その他の回答 (0 件)

カテゴリ

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