필터 지우기
필터 지우기

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by