フィルターのクリア

Get structure elements across fields

1 回表示 (過去 30 日間)
Oyeniyi
Oyeniyi 2014 年 12 月 28 日
コメント済み: Oyeniyi 2014 年 12 月 28 日
I have a 1-by-n structure with -m- fields. All the elements of the structure are single numbers.
I want to get all the elements across any particular 'row' of the structure as a vector.
For example, I need to access [struct(n).field1 struct(n).field2 ... struct(n).fieldm] without having to go through the individual fields.

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 12 月 28 日
編集済み: Mohammad Abouali 2014 年 12 月 28 日
% Making sample data
s(1).field1=1;
s(1).field2=2;
s(1).field3=3;
s(2).field1=4;
s(2).field2=5;
s(2).field3=6;
% converting to regular array.
sTable=struct2table(s);
sArray=table2array(sTable)
sArray =
1 2 3
4 5 6
alternatively, you can also use struct2cell and cell2mat
sCell=struct2cell(s);
sCell=squeeze(sCell);
sArray=cell2mat(sCell)'
sArray =
1 2 3
4 5 6
  1 件のコメント
Oyeniyi
Oyeniyi 2014 年 12 月 28 日
Thanks.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 28 日
編集済み: Azzi Abdelmalek 2014 年 12 月 28 日
Another way:
d=struct('f1',num2cell(randi(10,1,10)),'f2',num2cell(randi(10,1,10)),'f3',num2cell(randi(10,1,10)))
n=4
e=d(n)
nf=fieldnames(d)
for k=1:numel(nf)
v(k,1)=e.(nf{k})
end
  1 件のコメント
Oyeniyi
Oyeniyi 2014 年 12 月 28 日
Thanks; I was trying to avoid using a loop.

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

カテゴリ

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