フィルターのクリア

Access Data Within Structure Array

2 ビュー (過去 30 日間)
Jon
Jon 2022 年 11 月 2 日
コメント済み: Jon 2022 年 11 月 2 日
Hello,
I'm new to Structure Arrays, but believe it is what I'm looking for to help me with my script after reading advice to not dynamically name variables.
One question I had: If I have data like this:
g = [1,3,5];
A = [90 89 88 87];
B = rand(4,2);
C = 70;
s.a = g;
s.b = {A,B,C};
I understand if I want to access the second datapoint of g, I'd type:
s.a(2)
Which will give me '3'.
Let's say I want to access the number 89 within A through the Structure.
This gives me the whole A array:
s.b(1)
But is there something like this, where I can get the 89 directly?
s.b(1(2))
This doesn't work, but I'm trying to get the second number (89) of the first variable (A) within s.b.
Or can I not do this?
Thanks.
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 11 月 2 日
There is a way
g = [1,3,5];
A = [90 89 88 87];
B = rand(4,2);
C = 70;
s.a = g;
s.b = {A,B,C};
val=cellfun(@(x) x(2), s.b(1))
ans = 89
Jon
Jon 2022 年 11 月 2 日
Thank you very much

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

採用された回答

Voss
Voss 2022 年 11 月 2 日
編集済み: Voss 2022 年 11 月 2 日
g = [1,3,5];
A = [90 89 88 87];
B = rand(4,2);
C = 70;
s.a = g;
s.b = {A,B,C};
s.b{1}(2)
ans = 89
  1 件のコメント
Jon
Jon 2022 年 11 月 2 日
Thank you

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by