# For loop on a structure's field names

**URL:** https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573
**Category:** New to Julia
**Created:** [April 14, 2020, 4:47pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573 "2020-04-14T16:47:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 14, 2020, 4:47pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/1 "2020-04-14T16:47:07Z")

</div>

Hi everyone, hope you are all healthy.

I’m a mechanical engineering student and I come from MATLAB, so I am sorry for my heresies.

I have a structure with 13 fields, each of which is a structure with 10 fields, for example:

```julia
mutable struct b
   b1::some type
   b2::some type
   b3::some type
   ...
   b10::some type
end
struct a
   a1::b
   a2::b
   a3::b
   ...
   a13:b
end

```

I have to loop on the 13 fields to assign a new value at the field b4 for example, how can I do it in Julia?  
In MATLAB I used to create an array of strings containing the 13 field names of the struct a:

```julia
afieldnames = ["a1" "a2" ... "a13"]

```

and then with the following loop I can access the field of b4 for each field of a as:

```julia
for i = 1:13
   a.(afieldnames(i)).b4 = somevalue
end

```

I know I can use the fieldnames and then getfield functions as shown in this [https://discourse.julialang.org/t/how-to-write-a-fast-loop-through-structure-fields/22535](https://discourse.julialang.org/t/how-to-write-a-fast-loop-through-structure-fields/22535), but I’m not able to do that for structure inside of structures.  
Thank you all in advance, and I hope I haven’t broken too many rules!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [April 14, 2020, 5:05pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/2 "2020-04-14T17:05:21Z")

</div>

Welcome!

Before we get into _how_ to do this, are you sure this is actually what you want? You’re jumping through a _lot_ of hoops to do something that would be trivial to do with a regular old vector. For example, what if you were to just replace `a` with a `Vector{b}` and `b` with a `Vector{some type}`? Then you could loop over the elements of `a` without needing to do anything special. And, as a bonus, the resulting code will be shorter and much more efficient. I’m happy to provide more detail if that’s a direction you want to go in.

---

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 14, 2020, 7:22pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/3 "2020-04-14T19:22:51Z")

</div>

First of all thank you for the attention! It is true that julia comunity is very friendly!!  
Shurely I could do it with some vectors and matrices, but it would be messier I guess.

I am trying to implement the dempster’s model to perform some calculations on a human body inertial properties. In order to do that the body is divided in 14 segments, each of which has its own length, mass, centre of mass,…

The structure a mentioned above would be a collector of the segments, so:

```julia
struct a
   head&neck::b
   right fore arm::b
   right thigh::b
   .....
end

```

while the structure b a collector of the properties, so

```julia
mutable struct b
   segment length::float64
   segment mass::float64
   ....
end

```

The structure is mutable since some values of the field are calculated in a function, it works, but I dont know if it is the best way to do it.

Since a lot of calculations are the same for each segment, for example the length of the segment is related to the height of the subject, I would like to compute them in the for loop in the fields of the structure a.  
If there is a cleaner way to implement I would really appreciate your advices. My goal for this implemantation of the dempster’s model is to make it easy to read, not pure performance, since all the calculations can be done by hand in less than an hour.

Thank you very much!

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [April 14, 2020, 7:50pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/4 "2020-04-14T19:50:56Z")

</div>

Just to chime in here with my opinion: @rdeits has it right. This is probably much better suited for a vector of vectors or a matrix. You’ll get the advantages of optimized looping/indexing, for one, and I’ll bet you’ll get improved cache locality as well.

Might I suggest a 13 (row) x 10 (column) matrix of `some type`, where each row corresponds to a single `a` struct and the columns are elements in your `b` structs? I suggest this because Julia’s matrices are column-optimized and it sounds like you’re looping through a given element in `b` across all `a`s.

If you want labels for the arrays for easier indexing, check out [NamedArrays.jl](https://github.com/davidavdav/NamedArrays.jl) (disclaimer: I haven’t used it) or even [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl).

---

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 14, 2020, 8:30pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/5 "2020-04-14T20:30:19Z")

</div>

Hi thank you for your answer.

My issue with using matrices and vectors is that retrieving information for a single segment would be less clean since I would have to call the centre of mass, the moment of inertia, the mass, the length,…, while with the structure it would be done in a single line of code (a.arm would contain all the informations about the arm of the subject). By the way if it is really that bad to do what I meant to do I will switch to matrix.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 14, 2020, 8:31pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/6 "2020-04-14T20:31:53Z")

</div>

You could always write 1 function that given the matrix calls everything you need and bundles it up.

---

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 14, 2020, 9:00pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/7 "2020-04-14T21:00:07Z")

</div>

I came up with another good reason to have something similar to this.

Suppose that I have to perform some experimental tests on a set of specimens with different properties. In MATLAB I used to create a structure organized as follows:

```julia
struct infos 
   temperature::Float64
   time::Float64
   relativeHumidity::Float64
   loggedData1::Array{Float64,2}
   loggedData2::Array{Float64,2}
   ....
end

struct data
   specimen1::infos
   specimen2::infos
   specimen3::infos
   ...
   specimenN::infos
end

```

Then if I had to perform the same calculation for all the specimens I would use the for loop mentioned above. Should I do it in an other way?

Thnak you all

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 14, 2020, 9:20pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/8 "2020-04-14T21:20:02Z")

</div>

> [@francesco123212](#):
>
> Should I do it in an other way?

Maybe this was suggested but just

```julia
data = [speciment1, speciment2, ...]

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [April 14, 2020, 9:32pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/9 "2020-04-14T21:32:41Z")

</div>

> [@francesco123212](#):
>
> I am trying to implement the dempster’s model to perform some calculations on a human body inertial properties. In order to do that the body is divided in 14 segments, each of which has its own length, mass, centre of mass,…
> 
> The structure a mentioned above would be a collector of the segments, so:
> 
> ```julia
> struct a
> head&neck::b
> right fore arm::b
> right thigh::b
> .....
> end
> 
> ```
> 
> while the structure b a collector of the properties, so
> 
> ```julia
> mutable struct b
> segment length::float64
> segment mass::float64
> ....
> end
> 
> ```
> 
> The structure is mutable since some values of the field are calculated in a function, it works, but I dont know if it is the best way to do it.

If each “body” is some overall info about the body (height, weight, subject identity, etc…) and a list of segments, you could probably organize it as

```julia
struct Segment
    length::Float64
    mass::Float64
end
struct Body
    height::Float64
    segments::Vector{Segment}
end

```

and now `body.segments[i]` gives you the `i`-th segment. Note that you probably don’t need to make `Segment` mutable: you can just set the `i`-th element of the vector to a new `Segment` struct, without mutating the old one (it tends to be more efficient).

If `body.segments[i]` is a bit “unreadable” and you’d prefer to refer to the `i`-th segment by name (say `arm` or `thigh`) you can use (as suggested above) a data structure that is like an array, but also keeps a label, check out for example [GitHub - SciML/LabelledArrays.jl: Arrays which also have a label for each element for easy scientific machine learning (SciML)](https://github.com/SciML/LabelledArrays.jl) (there are many alternatives, I personally haven’t really used any so can’t offer much advice here).

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [April 14, 2020, 9:33pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/10 "2020-04-14T21:33:09Z")

</div>

> [@francesco123212](#):
>
> Should I do it in an other way?

Using @kristoffer.carlsson’s suggestion is, I think, the idiomatic way to do this. When you have a collection of like-typed objects, create some collection of them, whether it’s a vector, a set, or something else. In my experience, Julia can optimize better for collections than it can some arbitrary structure, and you ultimately end up with cleaner / more idiomatic code.

---

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 14, 2020, 10:46pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/11 "2020-04-14T22:46:35Z")

</div>

thanks a lot for your time, I will try this solution that to me looks the best!

---

<div class="post-metadata">

### Author: ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)
#### Post date: [April 15, 2020, 7:22pm UTC](https://discourse.julialang.org/t/for-loop-on-a-structures-field-names/37573/12 "2020-04-15T19:22:01Z")

</div>

I managed to implement it in this way. Thanks a lot!!
