How to convert a layered structure into arrays.
Mostrar comentarios más antiguos
I have a structure like this
A.a.b.first, A.a.b.second, A.a.b.third
A.c.d.first, A.c.d.second, A.c.d.third
A.e.f.first, A.e.f.second, A.e.f.third
I want to extract the fields "first", "second" and "third" into arrays so that one array has all the firsts, another has all the seconds, and a thrid and all the thirds. Is there a simulink method to use or a matlab function? I am currently brute forcing it in matlab code. That is,
B(1) = A.a.b.first
B(2) = A.c.d.first
B(3) = A.e.f.first
and so forth. So it would be much better to have a loop or something even more streamlined.
4 comentarios
Whoever gave you data in this form has a lousy sense of humor.... According to your pattern, there could only be 9 more steps., am I right? If so, that doesn't seem like such an onerous thing to type by hand.
B(4) = A.g.h.first
.
.
B(13) = A.y.z.first
John Petersen
el 18 de Mzo. de 2021
Editada: John Petersen
el 18 de Mzo. de 2021
Are the desired fields always at a fixed depth (4 fields deep in your example)? Also, if the field names are not a simple alphabetical progression, what decides the order of the elements in B(i)? We need to know the general pattern before a solution can be recommended.
John Petersen
el 18 de Mzo. de 2021
Editada: John Petersen
el 18 de Mzo. de 2021
Respuesta aceptada
Más respuestas (1)
A more automated option,
f=string(num2cell('a':'z'));
for i=1:13
B(i)=A.( f(2*i-1) ).( f(2*i) ).first;
C(i)=A.( f(2*i-1) ).( f(2*i) ).second;
D(i)=A.( f(2*i-1) ).( f(2*i) ).third;
end
1 comentario
John Petersen
el 18 de Mzo. de 2021
Editada: John Petersen
el 18 de Mzo. de 2021
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!