how to get struct array field as a vector?

32 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 12 de Oct. de 2015
Respondida: Tin Aye Chit Chit el 15 de Mayo de 2019
I have a struct array, and a field like data.age and I want to get the ages as a vector v for which I can write for example : v(v>18), because data.age(data.age>18) gives me an error message.
  1 comentario
Stephen23
Stephen23 el 12 de Oct. de 2015
Editada: Stephen23 el 12 de Oct. de 2015
Accessing fields of non-scalar structures is covered quite well in the documentation:
It is also useful to understand comma separated lists:

Iniciar sesión para comentar.

Respuesta aceptada

Thorsten
Thorsten el 12 de Oct. de 2015
age = [data.age];
agegt18 = age(age>18);

Más respuestas (2)

Walter Roberson
Walter Roberson el 12 de Oct. de 2015
v = [data.age];
data(v>18)
Or more compactly,
data([data.age]>18)
  2 comentarios
Mr M.
Mr M. el 12 de Oct. de 2015
Thanks, and what if the filed contains strings, and I want to select elements for which string equals to a given string? Something like this: data({data.gender}=='male')
Thorsten
Thorsten el 12 de Oct. de 2015
Editada: Thorsten el 12 de Oct. de 2015
Use
data(strcmp({data.gender}, 'male'))

Iniciar sesión para comentar.


Tin Aye Chit Chit
Tin Aye Chit Chit el 15 de Mayo de 2019
Please check the code, that code can't give me the answer I want.
for i = 1:1:n
V=index{i};
for j = 1:1:size(V,2)
S{i,j}.e = nodeArch.node(V(1,j)).energy;
S{i,j}.t = nodeArch.node(V(1,j)).type;
S{i,j}.g = nodeArch.node(V(1,j)).G;
end
end
The S have more column than V. What is wrong in that code?

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by