# Convert data from txt file to matrix

**URL:** <https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481>\
**Category:** General Usage\
**Created:** [December 9, 2022, 4:13pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481 "2022-12-09T16:13:36Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:13pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/1 "2022-12-09T16:13:36Z")

</div>

Hello, I have txt file the following format  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a8678b217837db653a7681de16156d3b7deae665.png)  
It three column with numbers. How can i read data from file and convert him to matrix [N1, N2, 1] where N1 length first column, N2 length second column and 1 is the number that matches for example -1.8 and 0.34999999999999987 i.e -0.00022855632034940893 ( this example from image). Also how can I generalize this to the matrix [N1, N2, 4] for another text file where 6 columns.  
Thank you in advance for your help

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 9, 2022, 4:14pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/2 "2022-12-09T16:14:44Z")

</div>

Try

```julia
using DelimitedFiles
readdlm(file.txt)

```

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:17pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/3 "2022-12-09T16:17:57Z")

</div>

Thank you,I tried using this package and get next result:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/4/f40fd1709f7f81bb32206612da73459ea5d483f7.png)  
this is a little not what I need.

I need a similar matrix  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/b/5b334cbcaca794e596f626208bed8671eda3c226.png)

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [December 9, 2022, 4:32pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/4 "2022-12-09T16:32:41Z")

</div>

It is not very clear what your target is. From your first screenshot, it looks that you have a file with N rows, each row having three columns. You can read that as a N \times 3 matrix, which is what @pdeffebach suggested.

You are saying that you want an array of shape `(N1, N2, 1)`, where `N1` and `N2` are the lengths of the first and second column, respectively, which is confusing since all columns have the same number of rows in your file.

Also, `(N1, N2, 1)` means that the last dimension is singleton, i.e., it can hold just a single value, and so (if a three-dimensional array is not required) it is essentially the same as an array of shape `(N1, N2)`. Are you sure you want that?

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:34pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/5 "2022-12-09T16:34:48Z")

</div>

Yes, i fixed the screenshot

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 9, 2022, 4:35pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/6 "2022-12-09T16:35:42Z")

</div>

And what adds to the confusion (I had exactly the same) is that in the screen-shot first and second columns are equal. Is this file supposed to contain `x y z` where `x` and `y` are some sort of coordinates?

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:36pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/7 "2022-12-09T16:36:56Z")

</div>

Yes, x and y are coordinates

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 9, 2022, 4:39pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/8 "2022-12-09T16:39:34Z")

</div>

So, why are they repeated, and so many times?

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:45pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/9 "2022-12-09T16:45:57Z")

</div>

I attached a screenshot from the wrong text file, I fixed it everywhere. Sorry.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 9, 2022, 4:48pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/10 "2022-12-09T16:48:02Z")

</div>

I do not understand what the problem is. It doesn’t look like you did what I suggested in your notebook.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:49pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/11 "2022-12-09T16:49:48Z")

</div>

First and second column are coordinates and need get matrix [N, N] where value take from third column

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 9, 2022, 4:53pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/12 "2022-12-09T16:53:12Z")

</div>

Is your question how to read in the data or how to transform the data you read in to the shape you want? They are different steps.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 4:55pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/13 "2022-12-09T16:55:40Z")

</div>

How to transform the data to need shape

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 9, 2022, 5:00pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/14 "2022-12-09T17:00:04Z")

</div>

I think I know what is asked but we need to know one more thing. Is the data already gridded? _i.e._ are points regularly spaced or still needs to be gridded (interpolated)?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 9, 2022, 5:06pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/15 "2022-12-09T17:06:24Z")

</div>

Find unique values of first column and sort. The length of that is N1. Create a Dict mapping those unique values to their index in the sorted vector. Do the same for the second column. The number of unique values in N2. Create a matrix N1xN2. Loop through rows looking up the matrix row index (r) from the first Dict using the first column value and matrix column index (c) from the second Dict using the second column value. Set the matrix element at r,c to the third column value.

Sorting is optional, but it is probably what you want.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 5:34pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/16 "2022-12-09T17:34:52Z")

</div>

Data is gridded. I can explain how this data is obtained, maybe it will help.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 9, 2022, 6:00pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/17 "2022-12-09T18:00:33Z")

</div>

@joa-quim  
I have started, ended value x and length range X = N. I create range for X from x\_start to x\_end with length = N. After this i get step from this range by range.step.

On the first step i calculate value y by function Y(x). Then i move to the right to x\_end with value y i.e i fixed y and move to x\_end with range.step.

On the second step i take next value x and calculate y(x), after this i move to the left to x\_start with step = -range.step, y(x) is fixed. After this i move to right i.e to x\_end with step = range.step, y(x) is fixed.

On the last step i.e x = x\_end calculating y(x) and moving to the left i.e x\_start

that is, I take a point x from the range, calculate the value of y(x) from it and move with a fixed y(x) to the left and right borders and so on to the very last value of x from the range.  
and at each such point, some calculations are still performed, which gives the third column

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 9, 2022, 6:02pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/18 "2022-12-09T18:02:38Z")

</div>

OK, so you may be lucky and the following GMT.jl command will figure everything out. First, rename the file to have a `*.xyz` extension. Then do

```julia
using GMT

G = grdconvert("/path/to/yourfile.xyz")

```

And you get a full grid type (origin, increments, coordinates vector, etc…).

If that fails, the solution I know is to use the `xyz2grd` module but that requires consulting the manual on the original GMT version because I’ve not ported that manual to Julia usage yet. Though some help is provided with `? xyz2grd`.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [December 22, 2022, 5:52pm UTC](https://discourse.julialang.org/t/convert-data-from-txt-file-to-matrix/91481/19 "2022-12-22T17:52:12Z")

</div>

Thank you!
