# Writing complex-entried vectors to text files

**URL:** <https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890>\
**Category:** New to Julia\
**Created:** [May 21, 2020, 4:41pm UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890 "2020-05-21T16:41:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [May 21, 2020, 4:41pm UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890/1 "2020-05-21T16:41:46Z")

</div>

How do I write a vector with `Complex{Float64}` type entries into a text file so that I may read it later ? `writedlm()` does not seem to work :

```julia
t = rand(Complex{Float64}, 3); 
writedlm("t.txt", t, ""); 
t2 = readdlm("t.txt")
3×3 Array{Any,2}:
 0.431139 "+" "0.6332339101461475im"
 0.589629 "+" "0.03915758218748211im"
 0.259354 "+" "0.22444070781836611im"

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2020, 10:52am UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890/2 "2020-05-22T10:52:45Z")

</div>

There is no standard for representing complex numbers in delimited files. I would just split them, eg

```julia
writedlm("/tmp/t.txt", hcat(real.(t), imag.(t)))
u = readdlm("/tmp/t.txt") |> x -> Complex.(x[:, 1], x[:, 2])
t == u # true

```

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [May 22, 2020, 7:23pm UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890/3 "2020-05-22T19:23:46Z")

</div>

It was useful to learn about this notation, where can I learn more about the use of the | separator in `readdlm` ? I have found a solution using `serialize` .  
On a separate note, would you consider it worthwhile to enable reading and writing f Complex valued arrays using readdlm ? Serializing makes the file unreadable using text editors.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 23, 2020, 5:46am UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890/4 "2020-05-23T05:46:42Z")

</div>

> [@iamsuddhasattwa](#):
>
> the use of the | separator in `readdlm` ?

I think you may misunderstand, the `|` above is in the `|>` operator, which just applies that anonymous function.

> [@iamsuddhasattwa](#):
>
> would you consider it worthwhile to enable reading and writing f Complex valued arrays using readdlm ?

No, I think that delimited formats should be kept simple. They are not equipped to support any kind of structure.

I would suggest you explore JSON-based solutions: some of them can serialize structures, and they are plain text. Look at the JSON packages in the registry.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 23, 2020, 1:31pm UTC](https://discourse.julialang.org/t/writing-complex-entried-vectors-to-text-files/39890/5 "2020-05-23T13:31:01Z")

</div>

> [@iamsuddhasattwa](#):
>
> `writedlm()` does not seem to work :

It works as long as you tell `readdlm` that you want to read complex numbers:

```julia
julia> t2 = readdlm("t.txt", ',', ComplexF64)
3×1 Array{Complex{Float64},2}:
 0.0019004307849912472 + 0.42546410422261594im
     0.710874046364316 + 0.12131057843728388im
     0.483710505641759 + 0.054679671784845896im

```

(Julia [supports the formats](https://github.com/JuliaLang/julia/pull/24713) `X+Yim`, `X+Yi`, and `X+Yj`: Julia, Matlab, and Python formats.)
