Filter löschen
Filter löschen

Access Data Within Structure Array

2 Ansichten (letzte 30 Tage)
Jon
Jon am 2 Nov. 2022
Kommentiert: Jon am 2 Nov. 2022
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 2 Nov. 2022
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 am 2 Nov. 2022
Thank you very much

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 2 Nov. 2022
Bearbeitet: Voss am 2 Nov. 2022
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

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by