Effacer les filtres
Effacer les filtres

Access Data Within Structure Array

2 vues (au cours des 30 derniers jours)
Jon
Jon le 2 Nov 2022
Commenté : Jon le 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 commentaires
Dyuman Joshi
Dyuman Joshi le 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 le 2 Nov 2022
Thank you very much

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 2 Nov 2022
Modifié(e) : Voss le 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

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by