# How to export a column vector to text file with writedlm function

**URL:** <https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818>\
**Category:** General Usage\
**Created:** [April 18, 2020, 8:27pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818 "2020-04-18T20:27:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fhkm](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fhkm](https://discourse.julialang.org/u/fhkm)\
**Post date:** [April 18, 2020, 8:27pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/1 "2020-04-18T20:27:48Z")

</div>

I want to export a column vector to text file.

> using DelimitedFiles  
> y=[1;2;3.4;5.5;7;8;45.78;0.445;76];  
> io=open(“x1.txt”,“w”) do io  
> writedlm(io,y)  
> end

After the codes is run, I did not get the result I expect. See the screenshot below:

 ![writedlm](https://global.discourse-cdn.com/julialang/original/3X/0/6/063d39d8052becfd5240f558a82babdfac1f5c25.png)

As shown in the screenshot, the variable is a column vector which is showing correctly in Excel (e.g., I use Excel to open the x0.txt Julia generated.). It is showing incorrectly in windows notepad program.  
What should I do to make the txt file look correct?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 18, 2020, 10:48pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/2 "2020-04-18T22:48:08Z")

</div>

If I understand correctly, you simply want to write a vector of numbers to a text file such that each number is followed by “\n”. If so:

```julia
io = open("test.txt", "w") do io
  for x in y
    println(io, x)
  end
end

```

would seem easiest. Or do I misunderstand what you are trying to do?

BTW, one would usually separate elements in a vector with “,” rather than “;”.

---

<div class="post-metadata">

**Author:** ![fhkm](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fhkm](https://discourse.julialang.org/u/fhkm)\
**Post date:** [April 18, 2020, 10:57pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/3 "2020-04-18T22:57:14Z")

</div>

I tried your suggested code. It gives the same results as mine.  
Please see the screenshot. I want the txt file to display the results in a column instead of a row.  
Specifically, it should have a new line command for each row.  
The screenshot show the txt file is a column when I use Excel to open it.  
But it is a row in the txt file.

 ![new1](https://global.discourse-cdn.com/julialang/original/3X/d/b/db51596dc7310b919e38954ad4b128d20646935c.png)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 18, 2020, 11:07pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/4 "2020-04-18T23:07:36Z")

</div>

Notepad is the “used napkin” of note-taking programs. Please use a real text editor and you will see that it works just fine!

---

<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:** [April 18, 2020, 11:09pm UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/5 "2020-04-18T23:09:26Z")

</div>

What version of Windows do you have? Prior to Windows 10, Notepad did not support `\n` Unix-style line endings, which is what Julia produces by default.

I would suggest getting a better text editor. vscode is free and fast, for example.

---

<div class="post-metadata">

**Author:** ![fhkm](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fhkm](https://discourse.julialang.org/u/fhkm)\
**Post date:** [April 19, 2020, 12:41am UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/6 "2020-04-19T00:41:37Z")

</div>

Thanks. I installed VS Code and it shows the result I expect.

---

<div class="post-metadata">

**Author:** ![fhkm](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fhkm](https://discourse.julialang.org/u/fhkm)\
**Post date:** [April 19, 2020, 1:00am UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/7 "2020-04-19T01:00:49Z")

</div>

For a 2D array, this code does not produce the output I expect:

```julia
julia> y=rand(9,4)
9×4 Array{Float64,2}:
 0.555096 0.00464277 0.0304166 0.843892
 0.678299 0.314659 0.283303 0.985769
 0.485882 0.576767 0.295104 0.669978
 0.327327 0.819604 0.134858 0.820752
 0.0562406 0.270076 0.346032 0.470565
 0.109073 0.668393 0.548311 0.217529
 0.244507 0.0839197 0.148862 0.0777072
 0.0740043 0.544063 0.608664 0.779534
 0.500565 0.699411 0.964454 0.164854

```

I want to print the above 2D array into txt file with the following code:

```julia
y=rand(9,4);
s=size(y);
io=open("a4.txt","w") do io
    for i=1:s[1]
        for j=1:s[2]
            if j < s[2]
                println(io, y[i,j],"\t")
            else
                println(io, y[i,j],"\n")
            end
        end
    end
end

```

Then I got this output in VS code. How can get this 2D array output correctly?

 ![2D array](https://global.discourse-cdn.com/julialang/original/3X/0/c/0cc5542bc2843313353a794c758556323f83e3d1.png)

---

<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:** [April 19, 2020, 1:15am UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/8 "2020-04-19T01:15:17Z")

</div>

Use `print`, not `println`; the latter appends a newline. Or just use `writedlm`

---

<div class="post-metadata">

**Author:** ![fhkm](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fhkm](https://discourse.julialang.org/u/fhkm)\
**Post date:** [April 19, 2020, 1:21am UTC](https://discourse.julialang.org/t/how-to-export-a-column-vector-to-text-file-with-writedlm-function/37818/9 "2020-04-19T01:21:42Z")

</div>

Thanks a lot for the help! Very appreciate it.
