Borrar filtros
Borrar filtros

Access Data Within Structure Array

2 visualizaciones (últimos 30 días)
Jon
Jon el 2 de Nov. de 2022
Comentada: Jon el 2 de Nov. de 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 comentarios
Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 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 el 2 de Nov. de 2022
Thank you very much

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 2 de Nov. de 2022
Editada: Voss el 2 de Nov. de 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

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by