16

I have text file which includes a matrix. I want to read it in julia as a matrix.

The text file is like:

0 0 0 0 0 0 0
1 0 0 0 0 0 0
1 0 0 0 0 0 1
1 0 0 0 1 1 0

In matlab you can do the following to create matrix M :

file='name.txt';
[M] = load(file);

How to do same thing in Julia?

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
hmi2015
  • 831
  • 3
  • 10
  • 22
  • 2
    Possible duplicate of [read into arrays in Julia](http://stackoverflow.com/questions/24295276/read-into-arrays-in-julia) – dasdingonesin Feb 10 '16 at 08:05

1 Answers1

24
shell> cat /tmp/m.txt
0   0   0   0   0   0   0
1   0   0   0   0   0   0
1   0   0   0   0   0   1
1   0   0   0   1   1   0

julia> m = readdlm("/tmp/m.txt")
4x7 Array{Float64,2}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  0.0  0.0  0.0  0.0  0.0  1.0
 1.0  0.0  0.0  0.0  1.0  1.0  0.0
daycaster
  • 2,655
  • 2
  • 15
  • 18
  • 3
    you can `readdlm` into Julia without having to `cat` the file first. – amrods Feb 10 '16 at 16:32
  • 4
    For anyone not familiar with julia: you need first to include DelimitedFiles as follows `using DelimitedFiles` in order to use `readdlm` – s.ouchene Nov 19 '21 at 11:36