matrixinputjuliafile-read

Reading data matrix from text file in Julia


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?


Solution

  • 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
    
    using DelimitedFiles
    
    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