Accessing elements of Structure

3 ビュー (過去 30 日間)
Kushagra
Kushagra 2013 年 12 月 18 日
コメント済み: Kushagra 2013 年 12 月 19 日
I have a structure which contains various arrays. I want to directly access a particular element of an array. My structure is as follows: s =
a: [1 2 3 4 5]
b: [1 2 3 4 5]
.
.
Now I want to access elements of these arrays(a,b...) in loop. So how can I access that. I am able to access complete array using below commands:
fields = fieldnames(s);
for i=1:length(fields)
s.(fields{i});
end
But I am not able to access a particular element directly in one command. How can I do that?

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2013 年 12 月 18 日
Simpler than you think I guess:
ElementsOfInterest = [2 5 1];
for i=1:length(fields)
s.(fields{i})(ElementsOfInterest(i));
end
Works for me.
HTH
  1 件のコメント
Kushagra
Kushagra 2013 年 12 月 19 日
I was using an additional dot to access elements of field.... Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by