# Loading/writing a single element from an array in JLD

**URL:** <https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414>\
**Category:** Data\
**Tags:** jld\
**Created:** [November 30, 2017, 4:27pm UTC](https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414 "2017-11-30T16:27:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel\_Gagge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axel_gagge/32/859_2.png) [@Axel\_Gagge](https://discourse.julialang.org/u/Axel_Gagge)\
**Post date:** [November 30, 2017, 4:27pm UTC](https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414/1 "2017-11-30T16:27:04Z")

</div>

I want to save and load data with JLD.jl (or HDF5.jl or something) which is is a 1-dimensional array of large elements (themselves large arrays). I want to be able to work with one or a few of the array elements at a time. Something like this for example:

```julia
file = jldopen("mydata.jld", "w")
y["a"][1] = rand(5) # does not work
close(file)

```

A simple solution would be to use the dictionary-like access also for the index elements:

```julia
save("tmp/test.jld", "variable_$i", v)
load("tmp/test.jld", "variable_$i")

```

Is there a better solution? According to the HDF5.jl documentation, it is possible to save a subset of an array. Can this also be accomplished with JLD?

EDIT: the problems I had were because I tried to create and overwrite a dataset all at once. If I use the array syntax, it is possible to modify the _contents_ of the dataset. This was unclear for me from the documentation, since there was no example showing this.

So this would not work:

```julia
using HDF5, JLD

jldopen("mydata.jld", "w") do file
    g = g_create(file, "mygroup")
    g["x"] = "foo"
end

jldopen("mydata.jld", "r+") do file
    g = file["mygroup"]
    g["x"] = "bar"
end

```

Nor does `g["x”][1] = "bar"`… While this works:

```julia
using HDF5, JLD

jldopen("mydata.jld", "w") do file
    g = g_create(file, "mygroup")
    g["x"] = ["foo", "bar"]
end

jldopen("mydata.jld", "r+") do file
    g = file["mygroup"]
    g["x"][:] = ["baz", "boo"]
end

```

This is probably all in the documentation. I’m just leaving this here to reduce confusion for others.

---

<div class="post-metadata">

**Author:** ![ksmcreynolds](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksmcreynolds/32/860_2.png) [@ksmcreynolds](https://discourse.julialang.org/u/ksmcreynolds)\
**Post date:** [December 1, 2017, 4:59pm UTC](https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414/2 "2017-12-01T16:59:03Z")

</div>

According to the [documentation](https://github.com/JuliaIO/JLD.jl/blob/master/doc/jld.md) you can `mmap` the data.

```julia
file = jldopen("mydata.jld", "r", mmaparrays=true)
y = read(file, "y") # y will be a mmapped array, not read immediately in its entirety

```

Then you can read individual elements or slices (e.g. `y[3][5:11]`).

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [December 1, 2017, 5:26pm UTC](https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414/3 "2017-12-01T17:26:26Z")

</div>

```julia
julia> a = jldopen(tempname(),"w")
Julia data file version 0.1.1: /tmp/juliaFzEqKh

julia> a["a"] = [rand(5),rand(4),rand(3)]
3-element Array{Array{Float64,1},1}:
 [0.138439, 0.79078, 0.977075, 0.271019, 0.236018]
 [0.514218, 0.823196, 0.647368, 0.869087]         
 [0.93793, 0.410167, 0.129439]                    

julia> a["a"][2]
1-element Array{Array{Float64,1},1}:
 [0.514218, 0.823196, 0.647368, 0.869087]

```

---

<div class="post-metadata">

**Author:** ![Axel\_Gagge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axel_gagge/32/859_2.png) [@Axel\_Gagge](https://discourse.julialang.org/u/Axel_Gagge)\
**Post date:** [December 3, 2017, 3:07am UTC](https://discourse.julialang.org/t/loading-writing-a-single-element-from-an-array-in-jld/7414/4 "2017-12-03T03:07:56Z")

</div>

This is very useful! But I was unclear in the title - I also need to write elements on the array. I can `mmap` to load the data but when writing I have to close and open the file again in write mode:

```julia
file = jldopen("mydata.jld", "w", mmaparrays=true)
file["x"][1] = ones(5) # does not work since we are in write mode!

# also this does not work since file is closed at some point
file = jldopen("mydata.jld", "r", mmaparrays=true)
x = file["x"]
close(file)
file = jldopen("mydata.jld", "w", mmaparrays=true)
file["x"] = x # ERROR: Error opening object //x
close(file)

```
