# Conversion from string to matrix

**URL:** https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109
**Category:** General Usage
**Tags:** parsing, complex-numbers, matrix
**Created:** [October 18, 2023, 9:27am UTC](https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109 "2023-10-18T09:27:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dnldlg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnldlg/32/209465_2.png) [@dnldlg](https://discourse.julialang.org/u/dnldlg)
#### Post date: [October 18, 2023, 9:27am UTC](https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109/1 "2023-10-18T09:27:08Z")

</div>

I saved a complex matrix to a text file, e.g. `ComplexF64[1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im]` but struggle to read it again. Is there an easy way to parse this?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 18, 2023, 9:43am UTC](https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109/2 "2023-10-18T09:43:00Z")

</div>

> [@dnldlg](#):
>
> ComplexF64[1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im]

Technically, you [could](https://docs.julialang.org/en/v1/manual/metaprogramming/) do:

```julia-repl
julia> eval(Meta.parse("ComplexF64[1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im]"))
2×2 Matrix{ComplexF64}:
 1.0+1.0im 1.0+0.0im
 5.0+0.0im 0.0+0.0im

```

However, you should be aware that parsing and evaluating arbitrary code is dangerous, especially if the strings come from an untrusted source.

---

<div class="post-metadata">

### Author: ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)
#### Post date: [October 18, 2023, 9:43am UTC](https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109/3 "2023-10-18T09:43:21Z")

</div>

Depends on your goals. If you want to persist data so that it can be used later, and you don’t care about human readability, then the easiest option is to use the `Serialization` library:

```julia
julia> using Serialization

julia> serialize("cpx.bin", ComplexF64[1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im])
64

julia> deserialize("cpx.bin")
2×2 Matrix{ComplexF64}:
 1.0+1.0im 1.0+0.0im
 5.0+0.0im 0.0+0.0im

```

If, on tthe other hand, you want to preserve human readability, you can use `eval` to parse the string:

```julia
julia> write("cpx.txt", string(ComplexF64[1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im]))
60

julia> eval(Meta.parse(read("cpx.txt", String)))
2×2 Matrix{ComplexF64}:
 1.0+1.0im 1.0+0.0im
 5.0+0.0im 0.0+0.0im

```

Note that calling `eval` on unsantiized strings from untrusted sources is dangerous, so only use this method if you are 100% certain that the contents of the text file are safe.

---

<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: [October 18, 2023, 12:15pm UTC](https://discourse.julialang.org/t/conversion-from-string-to-matrix/105109/4 "2023-10-18T12:15:33Z")

</div>

> [@HanD](#):
>
> If you want to persist data so that it can be used later, and you don’t care about human readability, then the easiest option is to use the `Serialization` library:

You can also just use comma-delimited text, e.g. via the `DelimitedFiles` library or several other libraries:

```julia
julia> using DelimitedFiles

julia> writedlm("cpx.csv", [1.0 + 1.0im 1.0 + 0.0im; 5.0 + 0.0im 0.0 + 0.0im], ',')

julia> readdlm("cpx.csv", ',', ComplexF64)
2×2 Matrix{ComplexF64}:
 1.0+1.0im 1.0+0.0im
 5.0+0.0im 0.0+0.0im

```

These have the advantage of being more human-readable and more portable across Julia versions.

See also the discussion thread: [Saving an array of complex numbers](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653)
