# Data Container like in Matlab Structure Arrays

**URL:** <https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691>\
**Category:** Data\
**Tags:** question\
**Created:** [March 10, 2022, 3:19pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691 "2022-03-10T15:19:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bozaga\_Murat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bozaga_murat/32/34441_2.png) [@Bozaga\_Murat](https://discourse.julialang.org/u/Bozaga_Murat)\
**Post date:** [March 10, 2022, 3:19pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/1 "2022-03-10T15:19:37Z")

</div>

Hello everybody,

I am master student in simulations in germany. I am newcomer from Matlab because the performance of Matlab is too low for my discretization in the additive manufactaring.

Julia is really nice, but I have a problem about the data container. I know structs and etc.

I used Structure Arrays in Matlab that was a easy way to manage the big data.  
Please, watch the picture.

 ![Structure_Array_Matlab](https://global.discourse-cdn.com/julialang/original/3X/7/4/740e3a4d7d254431b0843e8eae03447a55fb84da.png)

Its similar to structs but not the same in two points.  
I can’t hand over single variabels to a struct and the initialization is not the know way.

For example:

```julia
struct Person

age::Int64
Marks::Vector{Float64}

end

x = Person(4,zeros(2,))
# x = Person(4) is not possible

#What I need is above
# First Person
Person[1].age = 4;
Person[1].Marks = zeros(2,);
# Second Person
Person[2].age = 5;
Person[2].Marks = ones(2,);

```

Thanks for every help community.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 10, 2022, 3:41pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/2 "2022-03-10T15:41:49Z")

</div>

Hello and welcome to the community!

If you want to change the fields of a struct after it has been created, it must be declared mutable, i.e.,

```julia
mutable struct Person
    age::Int64
    Marks::Vector{Float64}
end

```

you can also set default values for the fields like so:

```julia
Base.@kwdef mutable struct Person
    age::Int64 = 0
    Marks::Vector{Float64} = Float64[]
end

```

after this you can create a vector of persons

```julia
persons = [Person() for _ in 1:2]
persons[1].age = 4;
persons[1].Marks = zeros(2,);
# Second Person
persons[2].age = 5;
persons[2].Marks = ones(2,);

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 10, 2022, 3:44pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/3 "2022-03-10T15:44:40Z")

</div>

You may also have a look at  
[https://github.com/JuliaArrays/StructArrays.jl](https://github.com/JuliaArrays/StructArrays.jl)

---

<div class="post-metadata">

**Author:** ![Bozaga\_Murat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bozaga_murat/32/34441_2.png) [@Bozaga\_Murat](https://discourse.julialang.org/u/Bozaga_Murat)\
**Post date:** [March 10, 2022, 6:14pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/4 "2022-03-10T18:14:21Z")

</div>

Thanks for you help. Have a nice day :).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 10, 2022, 6:25pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/5 "2022-03-10T18:25:14Z")

</div>

For performance reasons (if the solution above does not perform fast enough), you may want to use Immutable structs indeed, and in that case you need to “replace” the person in each position of your vector of persons.

```julia
julia> struct Person
           age::Int64
           Marks::Vector{Float64}
       end

julia> persons = [Person(4,zeros(4)), Person(5,zeros(4))];

julia> persons[1] = Person(6,persons[1].Marks) # replace first person using the previous fields

julia> persons
2-element Vector{Person}:
 Person(6, [0.0, 0.0, 0.0, 0.0])
 Person(5, [0.0, 0.0, 0.0, 0.0])

```

Note that the elements of the `Marks` are mutable in both cases (being the `Person` struct mutable or not).

---

<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:** [March 10, 2022, 7:27pm UTC](https://discourse.julialang.org/t/data-container-like-in-matlab-structure-arrays/77691/6 "2022-03-10T19:27:01Z")

</div>

> [@baggepinnen](#):
>
> If you want to change the fields of a struct after it has been created, it must be declared mutable

That of course is generally true, but in this scenario if one uses StructArrays, it may be possible to use an immutable structure. For example

```julia
julia> using StructArrays

julia> struct Person
           age::Int64
           Marks::Vector{Float64}
       end

julia> persons = StructArray([Person(4, zeros(4)), Person(5, zeros(4))])
2-element StructArray(::Vector{Int64}, ::Vector{Vector{Float64}}) with eltype Person:
 Person(4, [0.0, 0.0, 0.0, 0.0])
 Person(5, [0.0, 0.0, 0.0, 0.0])

julia> persons.age[1] = 10;

julia> persons
2-element StructArray(::Vector{Int64}, ::Vector{Vector{Float64}}) with eltype Person:
 Person(10, [0.0, 0.0, 0.0, 0.0])
 Person(5, [0.0, 0.0, 0.0, 0.0])

```

The trick here is that you are editing directly the component vector that holds the ages of all persons. (The structures themselves are generated on the fly when indexing into the `StructArray`.)
