# How do you get a Matrix back from a file?

**URL:** <https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154>\
**Category:** New to Julia\
**Tags:** question, file\
**Created:** [October 3, 2022, 12:17pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154 "2022-10-03T12:17:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [October 3, 2022, 12:17pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/1 "2022-10-03T12:17:43Z")

</div>

When you pass a Matrix to write like so

```julia
A = rand(UInt, 20,20)
write("mat.bin", A) 

```

It works just fine. No errors.  
But how do you get the matrix back out?  
The docs show the string example which leads one to try

```julia
Matrix(read("mat.bin"))

```

or

```julia
Matrix{UInt}(read("mat.bin"))

```

But both throw an error.

How do you get a `Matrix{UInt}` from a `Vector{UInt8}`?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 3, 2022, 1:07pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/2 "2022-10-03T13:07:09Z")

</div>

> [@AwesomeQuest](#):
>
> The docs show the string example

The `String` example is straightforward because UTF-8 already specifies how a bunch bytes (UInt8) should be interpreted to become a string. For a Matrix, such a straight conversion cannot work because the dimension information about the matrix isn’t stored anywhere, and is necessary to create it.

You can get the data as a vector of `UInt`s first with `reinterpret`:

```julia
julia> reinterpret(UInt, read("uints.bin"))
8-element reinterpret(UInt64, ::Vector{UInt8}):
 0x0000000000000001
 0x0000000000002710
 0x000000000000000a
 0x0000000000002694
 0x0000000000000064
 0x000000000000d431
 0x00000000000003e8
 0x0000000000008b7f

```

This was a dummy 2x4 `UInt` matrix I created and used `write` to write it into `"uints.bin"`. We can then use `reshape` to turn it back into a matrix, specifying the dimension sizes:

```julia
julia> reshape(reinterpret(UInt, read("uints.bin")), 2, 4)
2×4 reshape(reinterpret(UInt64, ::Vector{UInt8}), 2, 4) with eltype UInt64:
 0x0000000000000001 0x000000000000000a 0x0000000000000064 0x00000000000003e8
 0x0000000000002710 0x0000000000002694 0x000000000000d431 0x0000000000008b7f

julia> A #the original matrix
2×4 Matrix{UInt64}:
 0x0000000000000001 0x000000000000000a 0x0000000000000064 0x00000000000003e8
 0x0000000000002710 0x0000000000002694 0x000000000000d431 0x0000000000008b7f

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 3, 2022, 1:50pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/3 "2022-10-03T13:50:04Z")

</div>

> [@AwesomeQuest](#):
>
> ```julia
> A = rand(UInt, 20,20)
> write("mat.bin", A) 
> 
> ```
> 
> It works just fine. No errors.

It’s likely the wrong approach, it only stores the values, not dimensions here 20, 20, so it might as well be a vector of 400. Only do this in very few cases, where you know the dimensions of the data to be static (in case you want to write more to the file). What you likely want is some kind of serialization system, there are many, but this one works, and I think it may be the best one:

> **[GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia](https://github.com/JuliaIO/JLD2.jl)**
>
> HDF5-compatible file format in pure Julia. Contribute to JuliaIO/JLD2.jl development by creating an account on GitHub.

> [@digital\_carver](#):
>
> The `String` example is straightforward because UTF-8 already specifies how a bunch bytes (UInt8) should be interpreted to become a string.

Well neither is it straightforward (if you want to store more than one string in a file), because I checked and it stores the `String` exactly, in my test “Palli”, my name as 5 bytes, not even a trailing \0 or length, so `write`, and its docs are rather useless and misleading. I feel it’s missing a note, stating most should rather use JLD2.jl (and some software, likely it, might use write indirectly for you).

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [October 3, 2022, 2:56pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/4 "2022-10-03T14:56:29Z")

</div>

Can confirm, the docs are not very helpful in this area.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [October 3, 2022, 3:01pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/5 "2022-10-03T15:01:27Z")

</div>

For the most simple applications, you might also consider [`Serialization`](https://docs.julialang.org/en/v1/stdlib/Serialization/), which is in the standard library:

```julia
using Serialization
A = rand(UInt, 20, 20)
serialize("mat.jls", A)
B = deserialize("mat.jls")

```

This method is not stable across minor versions (1.7 vs 1.8, say), but for shorter term solutions I think it is pretty nice.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 3, 2022, 7:27pm UTC](https://discourse.julialang.org/t/how-do-you-get-a-matrix-back-from-a-file/88154/6 "2022-10-03T19:27:24Z")

</div>

> [@cgeoga](#):
>
> you might also consider [`Serialization`](https://docs.julialang.org/en/v1/stdlib/Serialization/) [..] This method is not stable across minor versions (1.7 vs 1.8, say)

That would be a good reason to avoid it then? But the situation is a bit more complex though:

> The data format can change in minor (1.x) Julia releases, but files written by prior 1.x versions will remain readable. The main exception to this is when the definition of a type in an external package changes. If that occurs, it may be necessary to specify an explicit compatible version of the affected package in your environment. Renaming functions, even private functions, inside packages can also put existing files out of sync. Anonymous functions require special care: because their names are automatically generated, minor code changes can cause them to be renamed. Serializing anonymous functions should be avoided

I don’t recall the pros and cons of all the options, should JLD2.jl me promoted over this?
