Access data in a structure array using index

2 ビュー (過去 30 日間)
Nathan
Nathan 2013 年 10 月 9 日
コメント済み: Azzi Abdelmalek 2013 年 10 月 9 日
After a bit of searching the internet I still haven't come up with a good way to do this without a for loop. I have a structure like the following:
S(1).x{1}=1;
S(1).name{1}='a';
S(1).x{2}=2;
S(1).name{2}='b';
S(2).x{1}=2;
S(2).name{1}='b';
[tf loc]=arrayfun(@(x)ismember('b',x.name),S)
This gives
loc=[2 1]
so I want to access S(1).x(2) and S(2).x(1). Is there a way to extract the data I want into an array without using a loop?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 9 日
Your question is not clear, the answer is in your question
S(1).x(2)
S(2).x(1)
Nathan
Nathan 2013 年 10 月 9 日
Is there some way to achieve it in a single line rather than accessing each S(i) individually?

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 9 日
編集済み: Azzi Abdelmalek 2013 年 10 月 9 日
If you want to extract all S.x
v={S.x}
% to have S(1).x(2)
v{1}(2)
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 9 日
There is no reason to avoid systematically a for loop.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by